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

A question about usage of Pointer

hillyuan
Beginner
492 Views
Defining the following tree structure with a pointer to an array inside:

TYPE :: node
DOUBLE PRECISION :: xmin,ymin,zmin
DOUBLE PRECISION :: xmax,ymax,zmax
TYPE(node), POINTER :: before
TYPE(node), POINTER :: next
INTEGER, DIMENSION(:), POINTER :: elein
END TYPE

An operation access violation error occurs in following code:
IF( ASSOCIATED(ptr%elein) ) THEN
n=SIZE(ptr%elein)
IF(n/=0) THEN
WRITE(nfile,*) (ptr%elein(i),i=1,n)
ENDIF
ENDIF

I found ptr%elein is in the state of undefined pointer/array, but it still goes into the if block. That means, the function ASSOCIATED didn't work.

Do anyone know what's the reason?
0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
492 Views
That's because you didn't initialize the pointer to NULL; pointer's default state in the beginning of its life is not NULLIFYed but undefined. It's even illegal to query ASSOCIATED() on undefined pointer (i.e. results are unpredictable, as you experienced).

The easiest workaround is to put F95 default-initialization into type-declaration:

TYPE :: node
DOUBLE PRECISION :: xmin,ymin,zmin
DOUBLE PRECISION :: xmax,ymax,zmax
TYPE(node), POINTER :: before =>NULL()
TYPE(node), POINTER :: next =>NULL()
INTEGER, DIMENSION(:), POINTER :: elein =>NULL()
END TYPE

Jugoslav
0 Kudos
hillyuan
Beginner
492 Views
It seems it does the reason. Thank you very much.
0 Kudos
Reply