Sizeof Fortran derived containing allocatable array

I am curious about the sizeof Fortran derived type.

type structInt
  integer :: n
  double precision :: f
end type structInt

type(structInt) :: a

A sizeof( a ) gives (on my system) a result of 16 bytes, which is fine considering padding is added.

But if there is an allocatable array inside the derived type, then it jumps to 64 bytes.

type structArray
  integer, dimension(:), allocatable :: arr
end type structArray
type(structArray) :: b

I couldn’t find in the Fortran standard why this behavior.. I expected that the size would be the size of the derived type to be the same size of the arr argument alone.

>Solution :

The Fortran standard doesn’t specify such implementation details, it’s up to the processor (compiler) to decide how the derived type is organized in memory (*)

Regarding the allocatable array, it’s not a simple address (as it would be in C) but rather a full descriptor (a hidden type, somehow) that contains also the rank, the sizes, lower bounds, upper bounds… So in your case 64 bits is plausible (again, the standard doesn’t specify how the descriptor is built).

(*) unless the sequence keyword is inserted before the component list. In that case, the compiler is required to store the components in the order of the list, and without internal padding (the type is then similar to a C struct).

Leave a Reply