f_size#

Return the number of elements of a scalar or an array as integer(f_size_t), without accessing any element.

use futile                                  ! or:
use f_utils, only: f_size                   ! or:
use f_size_module, only: f_size

f_size_module (flib/f_size.f90) defines the interface; f_utils and futile re-export it unchanged.

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_size.f90, which fypp/generate-sizeof.py generates from fypp/size.fypp). In the signature, any_intrinsic is a documentation placeholder: the argument may be of any intrinsic type, of any Futile ABI kind. The signature line merges both calling forms; their exact argument order is given in the description below and under Typical calls.

interface  f_size_module/f_size(x[, ln])#

Calling forms: f_size(x), for any ABI type/kind and rank 0-7, and f_size(ln, x), for character arrays (rank 1-7) only, with ln=len(x) as the first argument. The result is integer(f_size_t).

Parameters:

x [any_intrinsic, character(len=ln), in,rank 0-7] :: Scalar or array whose elements are counted: any intrinsic type, of any Futile ABI kind; rank 0–7. No element is read, also from pure procedures.

Options:

ln [integer, in] :: Character-array form f_size(ln, x) only: the element length len(x), passed as the first argument; f_size(x) gives the same result.

Typical calls#

n = f_size(array)              ! any ABI type/kind, rank 0-7
n = f_size(value)              ! 1 for any scalar
n = f_size('abc')              ! 1: a scalar string is one element
n = f_size(len(names), names)  ! character array, element length first
n = f_size(names)              ! the one-argument array form also works

Returns#

The element count as integer(f_size_t), a kind compatible with C size_t; Fortran still treats the integer as signed. A scalar, including a zero-length string, counts as one element. An array with any zero extent has zero elements. Noncontiguous sections count their payload elements, excluding stride gaps, descriptors and allocation overhead.

Supported kinds and ranks#

Scalar and array overloads cover the concrete f_precisions 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_size(ln, x).

111 specific procedures implement f_size (generated by FYPP; table emitted by the documentation stub f_size_doc.f90 from the same loops).

Type (kind)

ranks of x

ranks of ln

specifics

integer (f_int8, f_short, f_integer, f_long); real (f_simple, f_double, f_long_double); complex (f_simple, f_double, f_long_double); logical (f_byte, f_logical); character (len=*)

0-7

–

8 each

character (len=ln)

1-7

0

7

Notes#

No element access, pure

Array extents are obtained directly in f_size_t through shape(x, kind=f_size_t). No array element is read, and the inquiry can be called from pure procedures.

Representable counts

Let H = huge(0_f_size_t). An element count must be at most H; representability of every positive intermediate product is a precondition. Nonpositive extents yield zero before any multiplication, and two negative extents do not turn an empty shape into a positive count. f_size performs no overflow termination or error reporting, and no result, saturation or wrapping behaviour is promised outside that range. The byte-size limits are tabulated on the f_sizeof page.

Migration

The result kind changed from default integer to f_size_t: dependent Fortran code must be recompiled against the updated module files. Neither compilation nor the ordinary regression tests require FYPP.

Examples#

The example is compiled and run by CI (tests/flib/size_contracts.F90).

subroutine example_size_contracts()
  real(f_double) :: values(2,3)
  character(len=3) :: names(2)
  integer(f_size_t) :: bytes, elements

  values=0.0_f_double
  names='abc'
  elements=f_size(values)          ! 6 elements, independent of their type.
  bytes=f_sizeof(values)           ! 48 bytes on an eight-byte double ABI.
  call expect_size(elements,6_f_size_t,'element count')
  call expect_size(bytes,6_f_size_t*storage_size(0.0_f_double,kind=f_size_t)/8_f_size_t,'byte count')
  call expect_size(f_size(len(names),names),2_f_size_t,'two string elements')
  call expect_size(f_sizeof(len(names),names),6_f_size_t,'six character bytes')
  call expect_size(f_size('abc'),1_f_size_t,'one scalar string')
  call expect_size(f_sizeof('abc'),3_f_size_t,'three scalar character bytes')
end subroutine example_size_contracts

See also#

  • f_sizeof – payload storage in bytes, with the same kinds and ranks.

  • f_memcpy – n counts source elements, as returned by f_size.

  • f_subptr – borrowed rank-one views whose n counts elements.