- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Suppose we've defined a derived data type which includes an allocatable array.
[fortran] type textline
character(len=2)::atom_label
integer(kind=4)::atom
real(kind=8),dimension(:),allocatable::coord
end type textline
[/fortran]
Then we also define an allocatable array of this derived type.
[fortran] type(textline),dimension(:),allocatable::datum [/fortran]
Now the question is how to allocate memory for datum?
I've tried the following.
[fortran] integer(kind=4)::m,n,info m=10 n=3 allocate(datum(m)%coord(n),stat=info) if (info/=0) stop "Error." [/fortran]
ifort reports no error, however, there's a segment fault in run-time.
Does anyone have any experience on this issue?
Thank you in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Do it in two different steps:
[bash]ALLOCATE(datnum(m)) DO I = 1, m ALLOCATE(datnum(I)%coord(n)) END DO[/bash]
Think of it this way: datnum has to exist before the %coord part exists. So you create m datnums, then you create n coords inside each of the m datnums.
The segfault is because you are trying to access datnum%coord before any datnum's exist.
Tim
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Do it in two different steps:
[bash]ALLOCATE(datnum(m)) DO I = 1, m ALLOCATE(datnum(I)%coord(n)) END DO[/bash]
Think of it this way: datnum has to exist before the %coord part exists. So you create m datnums, then you create n coords inside each of the m datnums.
The segfault is because you are trying to access datnum%coord before any datnum's exist.
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you!
I've tried your approach, it works!
Actually, I've also tried this way.
[fortran]allocate(datum(m)) allocate(datum(m)%coord(n))[/fortran]However, it doesn't work. The %coord part is still not assigned in memory. Maybe the do loop is the only way to the problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What you posted there would work because after the first allocate on datum, there are m datum's.
The second allocate only allocates coord in the m-th datum. datum(1) to datum(m-1) still have not had their coord array allocated. The segfault happens when you try to access datum(1)%coord.
To do what you are trying to do, it would be allocate(datum(:)%coord(n)) for the second line. You can try this, but it won't work!
You can't pass only the components of the full array of derived types. In other words, the range specifier (ie. min:max where min and max are optional) has to appear after the last %. Meaning:
[fortran]TYPE myType1 REAL, DIMENSION(m) :: data END TYPE TYPE myType2 TYPE(myType1), DIMENSION(n) :: mt2 END TYPE TYPE(myType2), DIMENSION(o) :: mt CALL MySub(mt(:)%mt2(I)%data(:)) ! Wrong CALL MySub(mt(I)%mt2(:)%data(:)) ! Wrong CALL MySub(mt(I)%mt2(J)%data(:)) ! Right[/fortran]Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
To do what you are trying to do, it would be allocate(datum(:)%coord(n)) for the second line. You can try this, but it won't work!
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page