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

Allocating an allocatable element of an allocatable array of derived types

eos_pengwern
Beginner
410 Views

Forgive me for the convoluted title; all will hopefully become clear...

I have a derived type which contains a set of allocatable arrays:

[bash]    type :: Bin
    
        real(kind(1d0)), allocatable, dimension(:) :: abscissas, ordinates
    
    end type Bin[/bash]

I now proceed to create an allocatable array of these:

[bash]type(Bin), allocatable, dimension(:) :: lottabins

integer :: alloc_err

allocate(lottabins(1000), stat=alloc_err)
[/bash]


No problems so far; now I proceed to allocate the elements of each bin

[bash]do i=1,1000

allocate (lottabins(i)%abscissas(100), lottabins(i)%ordinates(100), stat=alloc_err)

if (alloc_err.ne.0) exit

end do
[/bash]


This fails while i=1 with an error "severe (151): Allocatable array is already allocated". It's as if the compiler thinks I'm trying to reallocate 'lottabins' itself when in fact what I'm trying to do is to allocate, for the first time,the arrays which are elements of lottabins(i).

Is there in fact a way of doing this, or I am trying to do something which isn't supported?

Stephen.

0 Kudos
3 Replies
Steven_L_Intel1
Employee
410 Views
Please show us a complete, buildable and runnable program that demonstrates the problem. I often find that when users show excerpts of their code, the problem is in code not shown.
0 Kudos
jimdempseyatthecove
Honored Contributor III
410 Views
From my recollection a rather old version of IVF had this problem. ca 8.n or 9.n.
What is your compiler version?

Jim Dempsey
0 Kudos
eos_pengwern
Beginner
410 Views
Oh, the shame! Yes, you were quite right, when I took the code excerpts I had given you and put them into a short console program, they worked well. When I broke down my (much more complex) real program and looked at it closely, I found a duplicate variable name that accounted for the error.

This is what you get for working late on a Labor Day weekend. I'm sorry to have troubled you and hope you enjoy the rest of yours.

Stephen.
0 Kudos
Reply