Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29285 Discussions

Memory usage with allocate and derived type

alexismor
Beginner
603 Views
Hello,

I have a derived type called particle that holds the quantum numbers of a partice like so:

type(particle)
integer :: m,n,s
end type particle

I then made another type to hold an array of these particles, along with another parameter called norm:

type(basis)
type(particle), allocatable :: p(:)
real*8 :: norm
end type basis

Finally, there are different ways of grouping the parameters together (there are multiple basis states) and I need to enumerate them all and remember the results. So in my main code I have something like this:

type(basis), allocatable :: b(:)

Now suppose that I have 10 basis states, and only 3 particles. The way I was initializing my b array is like:

allocate(b(10))
do i=1,3
allocate(b(i)%p(3))
enddo

However, I just noticed when using the debugger in Visual Studio that as soon as I allocate the b array, all the sub-arrays of particles are also allocated to the same size. So instead of ending up with

b(1)%p(1)
b(1)%p(2)
b(1)%p(3)
b(2)%p(1)
b(2)%p(2)
b(2)%p(3)
...
b(10)%p(1)

I get

b(1)%p(1)
...
b(1)%p(10)
b(2)%p(1)
...
b(10)%p(10)

am I doing anything wrong?

Thanks,

Alexis
0 Kudos
3 Replies
alexismor
Beginner
603 Views
It looks like this might just be an artifact created by MS Visual Studio. When I test a little program like:

program test
use basis_module
type(basis), allocatable :: b(:)

allocate(b(10))
allocate(b(1)%p(3))
print*,'size(b)=',size(b)
print*,'size(b(1)%p=',size(b(1)%p)
stop
end

I get the expected results.
0 Kudos
Zhanghong_T_
Novice
603 Views
I have the similar experience. It seems that in watch window the results displayed are wrong, but the final results of the program are correct.
0 Kudos
Steven_L_Intel1
Employee
603 Views
I can reproduce Alexis' problem and will let the developers know about it. It seems that the debugger is misinterpreting the information the compiler supplies.
0 Kudos
Reply