A question about usage of Pointer

A question about usage of Pointer

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?

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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

Jugoslav
www.xeffort.com

It seems it does the reason. Thank you very much.

Leave a Comment

Please sign in to add a comment. Not a member? Join today