Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading