🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #52307

The semantics of volatile wrt indirections and function ptrs

#52307
From: Jim Mattson
Date: Mon, 30 Nov 1992 04:55
111 lines
3447 bytes
The semantics of the volatile keyword have me confused.  I think I
understand the really basic cases:

volatile int i;		/* Don't optimize references to i; it may
			   be shared or it may be mapped to a device */

volatile void foo() 	/* Foo doesn't return */
{...}

However, I have (at least) three questions...

--------

(1) How do I control the binding of volatile wrt indirections?  Consider... 

volatile int *i;	

Is the volatile bound to the variable or to its contents:

volatile (int *i)
-or-
(volatile int *) i

Gcc warning messages regarding the dropping of volatile lead me to
believe the latter:

foo()
{
    volatile i;
    volatile int *vp;
    int *nvp;

    vp = &i;
    nvp = &i;
}

foo.c: In function `foo':
foo.c:8: warning: assignment discards `volatile' from pointer target type

So, suppose I want to get the former semantics?  For example, suppose I
have a pointer to an int that happens to live in shared memory, and I
want to make sure that each access actually fetches it from memory
(i.e. that the optimizer doesn't put it in a register)?

--------

(2) What about function pointers (as opposed to all other pointers)?

volatile void (*fp)(void);

Is the pointer itself volatile, or does it just point to a function
that doesn't return:

volatile (void (*fp)(void))
-or-
(volatile void (*fp))(void)

In this case, the lack of a warning leads me to believe the former:

volatile void foo(void)
{
    volatile void (*vp)(void);
    void (*nvp)(void);

    vp = foo;
    nvp = foo;
    nvp = vp;
    exit(0);
}

The compiler doesn't complain about any of these assignments, so it
seems that none of the pointers are to volatile objects.

Suppose I want to indicate the latter usage?  (i.e. I would like to
indicate to the optimizer that (*vp)() does not return, just as foo()
does not return.)  Is it possible?

--------

(3) Is the above reasoning correct?  Above, it appears that there is no way
to get a pointer to a (volatile function), although you can get a
(volatile pointer) to a function.  Why, then, does gcc warn of volatile
being dropped if the function pointer is implicitly cast to (void *):

volatile void foo(void)
{
    volatile void *vp;
    void *nvp;

    vp = foo;
    nvp = foo;
    exit(0);
}

foo.c: In function `foo':
foo.c:7: warning: assignment discards `volatile' from pointer target type

--------

It seems that the behavior in (1) and (3) is consistent, but (2) is not.
Unfortunately, the behavior I usually want is not the behavior of (1)
and (3).  I often have pointers in shared memory which are themselves
volatile...sometimes their targets are volatile, and sometimes not.
Can this be kludged with a structure wrapper:

volatile struct {int *i;} vp;

Does it have to be done like this, or is there a better way?

--jim
Technical Details
Message-ID: <9211302055.AA23078@odin.ucsd.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!cs.ucsd.edu!mattson

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,c744ab3a00a00d1,start
X-Google-Attributes: gid11318c,public
X-Google-ArrivalTime: 1992-11-30 13:04:33 PST
Newsgroups: gnu.gcc.help
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!cs.ucsd.edu!mattson
From: mattson@cs.ucsd.edu (Jim Mattson)
Subject: The semantics of volatile wrt indirections and function ptrs
Message-ID: <9211302055.AA23078@odin.ucsd.edu>
Sender: daemon@cis.ohio-state.edu
Organization: GNUs Not Usenet
Distribution: gnu
Date: Mon, 30 Nov 1992 04:55:08 GMT
Lines: 111