- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
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?
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It seems it does the reason. Thank you very much.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page