Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
123 Views

how to test if pointer array is allocated

I'm working on a code which has a large number of dynamically allocated arrays which are in defined TYPE's as pointer variables.  For example......

TYPE testtype
    real(8), pointer :: testarray(:)
END TYPE

At run time, %testarray is allocated with the ALLOCATE( ) function, and later freed with DEALLOCATE( ).

What is the correct way to test if the array is allocated?  If my understanding is correct, the compiler doesn't permit use of ALLOCATED( ) on pointer variables, and the fortran standard says SIZE( ) should not be used on unallocated arrays.  The code was using SIZE to do this, and that was failing, and when I replaced SIZE with ALLOCATED, the compiler said "error #6547: The ARRAY argument of the ALLOCATED inquiry intrinsic function shall be an allocatable array."

If there is no way to do this, should I replace "pointer" with "allocatable"?  There are tons of these, and I worry that this will break something.

0 Kudos
9 Replies
Highlighted
Black Belt
123 Views

Read about ASSOCIATED. Pointer variables were problematic in Fortran 90. In general, it is a good idea to use allocatable variables as much as possible instead of pointer variables in new code, and to convert code containing pointer variables.

0 Kudos
Highlighted
New Contributor I
123 Views

Thanks for the tip.  My memory is slipping.  Seems I asked nearly the same question 7 months ago.

https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/759235

I did some searching before posting, but I didn't find that thread.  Sigh!

This code makes heavy use of SIZE( ) to decide if an array which was ALLOCATE( )'d should be DEALLOCATE( )'d.  I suppose the proper way to do this test is with ALLOCATED( ) or ASSOCIATED( ), according to how it was declared.  Was there ever a time during Fortran's development when SIZE( ) was the recommended way to test if an array is currently allocated?

0 Kudos
Highlighted
123 Views

I need to ask the same question of my code.  Suppose you have a declaration

TYPE Foo
    INTEGER :: nFB
    TYPE (FooBar), DIMENSION(:), ALLOCATABLE :: fbInstance
    ...
END TYPE Foo

Is it worth it to declare the internal TYPE (FooBar) to be an array of POINTERs?  Suppose you wish to send this array of items to another class.  Would it help being declared as POINTERs?

0 Kudos
Highlighted
Black Belt
123 Views

Brian Murphy wrote:
This code makes heavy use of SIZE( ) to decide if an array which was ALLOCATE( )'d should be DEALLOCATE( )'d. 

The Fortran standard says that the argument of SIZE() shall not be an unallocated allocatable or a pointer that is not associated.

0 Kudos
Highlighted
Black Belt
123 Views

NotThatItMatters wrote:
Is it worth it to declare the internal TYPE (FooBar) to be an array of POINTERs?

In Fortran you cannot have an array of pointers, as you can in C. The closest to that would be a creat a user defined type that contains a pointer as one (or the only) component, and then declare an array of that type.

0 Kudos
Highlighted
New Contributor I
123 Views

In reply to #5.  If the standard has always said that ever since the SIZE function first appeared, then this code never should have used SIZE for that purpose.  I will put this on my "fix it" list.

0 Kudos
Highlighted
Black Belt
123 Views

Brian Murphy wrote:
In reply to #5.  If the standard has always said that ever since the SIZE function first appeared, then this code never should have used SIZE for that purpose.

It's there in the '95 standard. Do you know the identity of the compiler used by the developer of the code? If so, looking at the documents of that compiler, if it is still available, may provide some insight.

0 Kudos
Highlighted
123 Views

Sorry, I knew that.  I have several constructs in my code with the following:

TYPE Foo
    INTEGER, PRIVATE :: nFB
    TYPE (FooBar), DIMENSION(:), POINTER, PRIVATE :: pfbInstance
    ...
END TYPE Foo
TYPE (FooBar), DIMENSION(:), ALLOCATABLE, TARGET, PRIVATE :: fbInstance

where later the Association between fbInstance and pfbInstance is defined.  Is there an advantage in this approach over using the array fbInstance within TYPE Foo?

0 Kudos
Highlighted
New Contributor I
123 Views

In reply to #8.  Unfortunately no, I don't know who, when or what compiler was used.  Should not have been Powerstation.  Probably was Compaq or an early Intel version.

0 Kudos