- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have a question conserning the use of allocatables in derived types.
I have made this test case:
[fortran]program Dynamic_memory implicit none !-- Type1 type :: T_myType1 integer, dimension(:), allocatable :: myIntAll end type type(T_myType1) :: myType1_1 ! 1 pointer member type(T_myType1), dimension(10) :: myType1_2 ! 10 pointer members !-- Type2 type :: T_myType2 integer, dimension(1) :: myIntAll end type type(T_myType2), dimension(10) :: myType2_2 integer :: mem integer, dimension(:), allocatable :: myAlloc !-- body write(*,*) 'Welcome to Dynamic_memory 101' mem = sizeof(myType1_1) write(*,*) 'The sizeof myType1_1 is currently: ', mem mem = sizeof(myType1_2) write(*,*) 'The sizeof myType1_2 is currently: ', mem mem = sizeof(myType2_2) write(*,*) 'The sizeof myType2_2 is currently: ', mem read(*,*) end program[/fortran]
The output of this is:
Welcome to Dynamic_memory 101
The sizeof myType1_1 is currently: 36
The sizeof myType1_2 is currently: 360
The sizeof myType2_2 is currently: 40
Hence myType1_1 uses 36 bytes of memory without having allocated any room for myIntAll whereas myType2_2 only uses 40 bytes corresponding to the size of the 10 integers that I have made room for (I am using integer = integer(4)).
My originally thought was that I could save some memory by making the array myIntAll allocatable, however there seem to be some overhead involved in using allocatables in derived types that are not present with fixed size arrays. Is it possible to calculate this overhead or am I doing something wrong here?
Regards
Lars
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
In my case the array that I would put into the derived type would sometimes have size 2 and other times size 4 and in rare instances size 9. If I have to declare them as fixed-size I would have to declare them all as size 9 even though I only rarely need 9 entries.
The size of the descriptor according to the help file is (on IA-32) (6+3*x)*4 bytes, where x is the dimension of the array in the above example this equal 36 bytes.
But if the descriptor uses 36 bytes of memory, then there is no benefit in using deferred-shape arrays.
Regards
Lars
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page