Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28591 Discussions

Derived data types with allocatable components

Debanjan_Mukherjee
471 Views
Hello,

I think I have some flaws in my understanding of the derived data types and how the memory allocation happens for them. I am currently working on a problem where I have to create some form of a list with variable number of entries, and I am trying to create a data type for that using allocatable components as follows:

type boxList
integer, dimension(:), allocatable:: boxTuples
integer, dimension(:), allocatable:: boxMembers
end type boxList

type(boxList), dimension(:),allocatable:: myboxList

1. One of the reasons why I wanted to have the components allocatable - is because I was intending on performing something like a 'push_back()' operation in C++ on my vector boxMembers to keep adding entries. But that leavse me with an allocatable array of derived types with allocatable components - and I am not sure how the memory assignment works here.

2. Could I first allocate the array myboxList
and then for each list - I go in and allocate the individual members ?

3. Also, is there any way in which I can define a constant number at runtime - something like ./executible.out "constant-number"
like we can do with c++ codes ? I ask this because if so, then I can fix the dimension of one of the member vectors by writing for example ./executible.out 3 for a 3 dimensional problem - (effectively for my physics simulator, this is the number of space dimensions I am working with). I guess this would be the equivalent of main(int argc) or something.

Any pointers will be very helpful to aid my understanding of these derived types.
Thank you.
0 Kudos
1 Reply
Steven_L_Intel1
Employee
471 Views
When you have an array of derived types with allocatable components, yes you need to allocate the parent array first and then allocate the components. I am not familiar with push_back in C++. I will note that you can simply assign to allocatable arrays and have the allocation happen automatically if you add -standard-semantics.

I don't know how you do what you're discussing in C++ - it sounds to me as if you have something that reads the argument list. This is hardly a "constant". But you can obtain a value from the argument list (as a string) using GET_COMMAND_ARGUMENT and then decode it as you wish, then use the value in an ALLOCATE statement.
0 Kudos
Reply