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

question about pointers and allocate

Brian_Murphy
New Contributor II
423 Views

A code I'm working on has the following:

real(8), pointer :: arr(:)

allocate( arr(100) )

if( allocated(arr) ) then

and the compiler says Error 1  error #6547: The ARRAY argument of the ALLOCATED inquiry intrinsic function shall be an allocatable array.

Why can arr be allocated, but then not checked with allocated()?  The compiler doesn't complain about deallocate(arr).

Is there a good reason why the person who wrote this code declared arr as a pointer instead of real(8), allocatable :: arr ?

I should also say that I've simplified this from how the code really is.  The variable arr is actually a member of a defined type instead of a standalone variable.

0 Kudos
2 Replies
Steve_Lionel
Honored Contributor III
423 Views

This trips up a lot of people. What you want is ASSOCIATED, not ALLOCATED, since a POINTER can be pointer-assigned,

I might suggest, though, that the better fix is to replace POINTER with ALLOCATABLE, unless the program uses pointer assignment. Many programmers automatically pick POINTER because they're used to that from other languages, or because they needed to deal with a restriction in F90/F95 that you couldn't have an ALLOCATABLE component of a derived type.

0 Kudos
Brian_Murphy
New Contributor II
423 Views

Thank bunches, Steve.  That change got the code running.

This code uses POINTERS in a lot of places where they are not needed.  Everyplace they wanted to allocate an array of a defined Type, they created two defined types.  One, say DTypeA, contained only a pointer variable to the other one, say DTypeB, which contains all the meat.  I see that as yucky because it adds an unneeded level of redirection.  DTypeA%DTypeB(i)%whatever.

I picked one pair of these, and eliminated DTypeA.  Then went through the code and changed all declarations of DTypeB to  allocatable, and edited all usage of DTypeB to remove the DTypeA% prefix.  That snowballed into more work where the array was passed in an argument list to subroutines.  The compiler said a formal explicit INTERFACE had to be provided if the argument list contains allocatable arrays of defined types with assumed size.  Not a bad thing to have explicit interfaces, but this code needs work in too many other more urgent places.

0 Kudos
Reply