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

Allocatable Arrays and Derived Types

Donald_K_
Beginner
441 Views
I heard mentioned that Intel Fortran 8 now supports the use
of allocatable arrays within derived types. More specifically,

type cell
real,allocatable,dimension(:,:,:):: q
end type cell
type(cell),allocatable:: vv(:)
. . .
allocate(vv(nval))
allocate(vv(n)%q(imx(n),jmx(n),kmx(n)))

was previously illegal syntax, forcing one to use "pointer" instead of "allocatable" in the above definition. (side note: why ??) While pointers provide flexibility for other example applications (ability to point to lots potential targets, etc), in the above example they seem a "necessary evil" as they can also reduce memory access efficiency.


Q: Is this just a syntax change or will there be a corresponding memory access benefit?
0 Kudos
1 Reply
Steven_L_Intel1
Employee
441 Views
Why was it illegal? Because the standard didn't provide for it.
The benefit of using ALLOCATABLE here is when you do things such as:
w = x
where w and x are derived type containing allocatable components. With pointers, you would have had the pointer copied, so you now had two pointers to the same data. With allocatable, the compiler first deallocates the component on the left if necessary, allocates the space required by the component on the right, and then copies the data. This helps avoid memory leaks and dangling pointers.
More specifically, one can't safely implement a package such as ISO_VARYING_STRINGS with pointer components.
The other aspect of this feature is that dummy arguments can be allocatable, and so can function return values.
I would not say that there is a "memory access benefit", but for some applications, there is definitely a "correctness" and "ease of implementation" benefit.
0 Kudos
Reply