f_subptr ======== Return a borrowed rank-one pointer into existing contiguous storage, without copying or allocating payload. .. code-block:: fortran use futile ! or: use f_subptr_module, only: f_subptr Signature --------- Generated from the FYPP documentation stub ``doc/generated/f_subptr_doc.f90`` (emitted by ``fypp/subptr-doc.fypp`` from the same loops as ``flib/f_subptr.f90``). In the signature, ``any_intrinsic`` is a documentation placeholder: the argument may be of any intrinsic type, of any Futile :ref:`ABI kind `. The supported combinations are listed under `Supported kinds and ranks`_. .. f:autointerface:: f_subptr Typical calls ------------- Use the source as the first positional argument and keyword window controls. Legacy positional forms differ between specifics. .. 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) .. rubric:: Selector rules ``n``, ``size``, ``shape`` and ``region`` are mutually exclusive; ``region`` cannot accompany ``from``. Integer controls accept ``f_integer`` and ``f_size_t``, including mixed combinations. ``region`` is built with ``first.to.last`` (``.to.`` is exported by ``futile``). Returns ------- A rank-one pointer with the source's intrinsic type and kind; character views preserve the element length. With no explicit selector the extent is the remaining flattened element count; otherwise it is the selected count. Supported kinds and ranks ------------------------- .. fortran-specifics:: f_subptr :args: ptr|ptr_addr Notes ----- .. rubric:: Ownership 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. .. rubric:: Contiguity Noncontiguous array sections are rejected rather than packed into temporary storage. For zero-size sections of nonempty parents, contiguity can be processor dependent; whole empty arrays are supported. .. rubric:: 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 :doc:`f_memcpy`. A null pointer must not be passed as an ordinary assumed-shape argument or used as a bounds-remapping target. Do not query an empty view's first or last element address. .. rubric:: Bounds and multidimensional views Native pointer bounds remapping gives a borrowed view its desired rank. ``shape=`` validates the selected element count; the bounds on the left of ``=>`` determine rank and extents. Use lower and upper bounds on every result dimension. For an explicit window that may be empty, check ``associated`` *before* remapping, even when the requested shape has zero total size: bounds remapping requires an associated, defined pointer target (see the pointer-assignment rules in `WG5 N2014 `_). .. rubric:: 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. A null pointer alone does not distinguish an error from a valid explicit empty window: inspect the Futile error status. Raw scalar-source capacities remain the caller's responsibility. .. rubric:: Deprecated ``f_subptr2`` ``f_subptr2`` remains a compatibility interface for default-integer rank-one sources and rank-two results. Use ``f_subptr`` plus native bounds remapping instead. No runtime warning is emitted, and it has no empty-window guarantees. Examples -------- All examples are compiled and run by CI (``tests/flib``). .. tab-set:: .. tab-item:: Flatten and copy Flattening/copy example, including nondefault owner bounds: .. fortranliteral:: tests/flib/subptr_edges.f90 :marker: subptr_views .. tab-item:: Remap (nonempty) .. fortranliteral:: tests/flib/subptr_remap.f90 :marker: subptr_remap_nonempty .. tab-item:: Remap (guarded) Guard an explicit window that may be empty before remapping: .. fortranliteral:: tests/flib/subptr_remap.f90 :marker: subptr_remap_guarded .. tab-item:: Remap (empty) Take the implicit whole view of an empty array to get an associated empty multidimensional view: .. fortranliteral:: tests/flib/subptr_remap.f90 :marker: subptr_remap_empty See also -------- * :doc:`f_subptr_str` -- the same windows counted in **characters**, for strings and C buffers. * :doc:`f_memcpy` -- copy raw storage, including into a borrowed view. * :doc:`f_memcpy_bytes` -- byte-count copies between scalar buffer starts. * :doc:`f_size` -- element count of an owner, to bound ``from`` / ``n`` windows. * ``f_malloc_ptr`` -- allocate a persistent pointer owner for views (:doc:`/memory`).