f_subptr — borrowed array views =================================== .. f:function:: f_subptr(ptr, n, from, lbound, shape, region, size) :module: f_subptr_module Return a rank-one pointer into existing contiguous storage, without copying or allocating payload. Available through ``use futile`` or ``use f_subptr_module, only: f_subptr``. Synopsis -------- Use the source as the first positional argument and keyword window controls. Legacy positional forms differ between overloads. .. code-block:: fortran flat => f_subptr(source) flat => f_subptr(source, from=offset, n=count) flat => f_subptr(source, shape=extents) flat => f_subptr(source, region=first.to.last) Arguments --------- .. list-table:: :header-rows: 1 :widths: 16 24 60 * - Argument - Type / default - Meaning * - ``ptr`` - Intrinsic array, rank 1--7; required - Persistent contiguous ``TARGET`` or ``POINTER`` of a supported ``f_precisions`` ABI kind. Numeric, logical, complex and character families are supported. Its original lower bounds do not affect window offsets. * - ``ptr_addr`` - Intrinsic scalar; alternative source - Legacy scalar-source keyword, not a numeric address. The scalar is the beginning of a raw contiguous buffer. An explicit window is required; the caller guarantees backing capacity and lifetime. A scalar string counts as one string element, not its individual characters. * - ``n`` - Integer scalar; optional - Nonnegative number of source elements. For array sources, omission of all window selectors selects the complete array or the suffix at ``from``. * - ``size`` - Integer scalar; optional - Legacy spelling of ``n``. Do not supply both. * - ``shape`` - Integer vector, zero to seven extents; optional - Selects the product of nonnegative extents. The result still has rank one. A zero extent gives an explicit empty window; a zero-length vector has product one. Shape multiplication is checked in ``f_size_t``. * - ``from`` - Integer scalar; default 1 - One-based element offset in the flattened source, in Fortran array-element order. Must be positive. Cannot accompany ``region``. * - ``lbound`` - Integer scalar; default 1 - Lower bound of the rank-one result. Does not change source selection. * - ``region`` - ``type(array_bounds)``; optional - Inclusive first/last element offsets, constructed with ``first.to.last``. The ``.to.`` operator is exported by ``futile``. Reversed endpoints give an explicit empty window. Integer controls accept ``f_integer`` and ``f_size_t``, including supported mixed-kind combinations. ``n``, ``size``, ``shape`` and ``region`` are mutually exclusive selectors. Prefer keywords rather than relying on legacy positional ordering. Result and ownership -------------------- The result has the source's intrinsic type and kind; character views preserve its element length. For an array with no explicit selector the extent is the remaining flattened element count. Otherwise it is the selected count. Writes through the result modify the owner. The owner must outlive every view; do not pass temporary expressions. Never ``deallocate`` or ``f_free`` a view. Nullify aliases before freeing their owner. Noncontiguous array sections are rejected rather than packed into temporary storage. Empty windows ------------- .. list-table:: :header-rows: 1 * - Request - Pointer result * - Explicit zero ``n`` or ``size``, zero shape product, or empty ``region`` - Disassociated (null), including for a scalar buffer start. * - Whole empty array with no explicit selector - Associated zero-size rank-one view; owns no allocation. * - ``shape`` with no entries - One element, because the product of zero factors is one. An associated zero-size view is a valid array argument, including for ``f_memcpy``. A null pointer must not be passed as an ordinary assumed-shape array argument or used as a pointer bounds-remapping target. Do not query an empty view's first or last element address. For zero-size sections of nonempty parents, contiguity can be processor dependent; whole empty arrays are supported. Multidimensional views ---------------------- Native pointer bounds remapping gives a borrowed view its desired result rank. ``shape=`` validates the selected element count; the bounds on the left of ``=>`` determine the rank and extents. Match those extents to the selected count. Use lower and upper bounds on every result dimension. The result follows Fortran array-element order and neither allocates nor copies data. .. fortranliteral:: tests/flib/subptr_remap.f90 :marker: subptr_remap_nonempty For an explicit window that may be empty, first obtain the flat pointer and check ``associated``. This guard must precede remapping, even when the requested multidimensional shape also has zero total size. This is a language requirement, not a compiler workaround: bounds remapping requires an associated, defined pointer target. See the pointer-assignment rules in the `WG5 working draft N2014 `_. .. fortranliteral:: tests/flib/subptr_remap.f90 :marker: subptr_remap_guarded If an associated empty multidimensional view is wanted, take the implicit whole view of an actual empty array and remap that associated pointer instead. .. fortranliteral:: tests/flib/subptr_remap.f90 :marker: subptr_remap_empty Errors ------ Invalid selectors, negative sizes/extents, invalid offsets, capacity violations, overflow and noncontiguous nonempty sources raise ``ERR_INVALID_MALLOC``. The result is null when the error is caught. Inspect the Futile error status; a null pointer alone does not distinguish an error from a valid explicit empty window. Raw scalar-source capacities remain the caller's responsibility. Examples and related APIs ------------------------- The original flattening/copy example also covers nondefault owner bounds: .. fortranliteral:: tests/flib/subptr_edges.f90 :marker: subptr_views See :doc:`f_subptr_str` for character-unit and C-string views, and :doc:`memory` for allocation, raw copying and size inquiries. Deprecated f_subptr2 -------------------- ``f_subptr2`` remains a compatibility interface for default-integer rank-one sources and rank-two results. It is deprecated in favor of ``f_subptr`` plus native bounds remapping. New code should use the checked pattern above; liborbs' packed compression tables now use it. No runtime warning is emitted. This deprecation does not add empty-window guarantees to the legacy helper.