f_sizeof ======== Return the payload storage of a scalar or an array in C bytes, as ``integer(f_size_t)``, without accessing any element. .. code-block:: fortran use futile ! or: use f_utils, only: f_sizeof ! or: use f_sizeof_module, only: f_sizeof ``f_sizeof_module`` (``flib/f_sizeof.f90``) defines the interface; ``f_utils`` and ``futile`` re-export it unchanged. ``f_precisions`` no longer provides it: code importing ``f_sizeof`` only from ``f_precisions`` should import ``f_sizeof_module`` instead. Signature --------- Generated from the FYPP documentation stub ``doc/generated/f_size_doc.f90`` (emitted by ``fypp/size-doc.fypp`` from the same loops as ``flib/f_sizeof.f90``, which ``fypp/generate-sizeof.py`` generates from ``fypp/sizeof.fypp``). In the signature, ``any_intrinsic`` is a documentation placeholder: the argument may be of any intrinsic type, of any Futile :ref:`ABI kind `. The signature line merges both calling forms; their exact argument order is given in the description below and under `Typical calls`_. .. f:autointerface:: f_sizeof Typical calls ------------- .. code-block:: fortran bytes = f_sizeof(array) ! payload bytes, any ABI type/kind, rank 0-7 bytes = f_sizeof(value) ! scalar byte width bytes = f_sizeof('abc') ! 3: one byte per character bytes = f_sizeof(len(names), names) ! character array, element length first bytes = f_sizeof(names) ! the one-argument array form also works Returns ------- The payload size in C bytes as ``integer(f_size_t)``, a kind compatible with C ``size_t``; Fortran still treats the integer as signed. A zero-length string and an array with any zero extent have zero bytes. Noncontiguous sections count their payload only, excluding stride gaps, descriptors and allocation overhead. For ``character(len=3) :: a(2)``, ``f_sizeof(a)`` is six bytes on the supported C-character platforms, while :doc:`f_size` counts two elements. Supported kinds and ranks ------------------------- Scalar and array overloads cover the concrete ``f_precisions`` :ref:`ABI kinds ` and ranks zero through seven; semantic kind aliases do not add duplicate overloads. The ``character(len=ln)`` row is the character-array form ``f_sizeof(ln, x)``. .. fortran-specifics:: f_sizeof :args: x, ln Notes ----- .. rubric:: Byte widths Byte widths use storage inquiries rather than assuming that a kind number denotes a byte width. Scalar widths are ``storage_size`` divided by the bit width of one C character, without hard-coding eight. Array sizes are the product of :doc:`f_size` and the matching scalar width. No array element is read, and the inquiry can be called from pure procedures. The generated ``size_inquiries`` regression checks the results against ``f_loc`` address differences and calls every kind/rank inquiry from a pure function. .. rubric:: Representable sizes Let ``H = huge(0_f_size_t)`` and ``B`` be the bytes per element. A byte size is representable when the element count is at most ``H / B`` (integer division); representability of every positive intermediate and final product is a precondition. Nonpositive factors yield zero before any multiplication, and empty arrays never overflow a temporary product. ``f_sizeof`` performs no overflow termination or error reporting; no result, saturation or wrapping behaviour is promised outside that range. Regression tests cover the largest representable arithmetic values and empty/negative extents without allocating giant buffers. For signed 64-bit ``f_size_t``, ``H`` is 9,223,372,036,854,775,807. The first overflowing element counts for ordinary storage widths are: ================= ========================= ========================= Bytes per element First overflow, 64-bit First overflow, 32-bit ================= ========================= ========================= 1 9,223,372,036,854,775,808 2,147,483,648 4 2,305,843,009,213,693,952 536,870,912 8 1,152,921,504,606,846,976 268,435,456 16 576,460,752,303,423,488 134,217,728 ================= ========================= ========================= Thus an eight-byte real array first overflows at 2**60 elements on a 64-bit kind: 8 EiB (2**63 bytes), for example a hypothetical cubic array with 1,048,576 elements along each axis. With a 32-bit kind, the first overflow is already 2 GiB of payload. Real address-space, memory, compiler descriptor and allocator limits generally prevent such allocations much earlier on 64-bit systems: these are arithmetic limits, not supported allocation sizes. C's unsigned ``size_t`` may represent larger values than the corresponding signed Fortran integer; the upper unsigned half is outside this API contract. .. rubric:: Migration The scalar result kind changed from default integer to ``f_size_t``: dependent Fortran code must be recompiled against the updated module files. Examples -------- All examples are compiled and run by CI. .. tab-set:: .. tab-item:: Elements and bytes From ``tests/flib/size_contracts.F90``: .. fortranliteral:: tests/flib/size_contracts.F90 :marker: size_contracts :dedent: 2 .. tab-item:: Scalar byte widths From ``tests/flib/f_precisions_examples.f90``: .. fortranliteral:: tests/flib/f_precisions_examples.f90 :marker: f_sizeof :dedent: 2 See also -------- * :doc:`f_size` -- element counts, with the same kinds and ranks. * :doc:`f_memcpy_bytes` -- copies whose ``nbytes`` usually comes from ``f_sizeof``. * :doc:`f_memcpy` -- element-count copies; capacities are checked in bytes.