VOLATILE on POINTER

VOLATILE on POINTER

The documentation is a little unclear on this, or perhaps I'm mis-reading it.  If I were to declare a variable as a POINTER, and then mark it as VOLATILE, is this marking the pointer as VOLATILE, or the memory region that it points to?  I need to mark the latter.  In this example, I have a structure, BAR, which I want to be able to grow in size as the program progresses, and I have already mapped the HMM common area to global shared memory between multiple processes, as well as the contents of BAR.  When I expand the array, I call a routine that remaps the new addresses of BAR to global, but since they're in shared memory, I need to mark the entire BAR structure as VOLATILE, not just the pointers.

STRUCTURE   /FOO/
    INTEGER*4   A
    INTEGER*4   B
    INTEGER*4   C
END STRUCTURE

BYTE    BEG_ADR
INTEGER*4    NUM_ALLOCATED
INTEGER*4    INDEX
BYTE    END_ADR
TYPE(FOO), POINTER :: BAR(:)

COMMON/HMM/
&  BEG_ADR,
&  NUM_ALLOCATED,
&  INDEX,
&  BAR,
&  END_ADR
VOLATILE/HMM/
VOLATILE BAR

 

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

According to the description in the standard, it effectively marks both.

[If you think about it, if a compiler has to allow for a change in the address stored in a pointer, that means that the pointer may point to different content making the value of the content different, so marking the address as volatile necessarily makes the content also volatile, but the standard is explicit in saying that both the value the association status may be changed by means not specified in the program.]

You will have to be careful in writing your access procedures such that they are thread-safe (inter-process as well as intra-process). Consider what happens when the pointer changes in the middle of:

BAR(I)%A = BAR(I)%B + BAR(I)%C

The A, B and C could potentially be members of different objects. Not to mention the other issue of atomicity.

Jim Dempsey

Leave a Comment

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