[comp.lang.c] volatile keyword - what does it mean?

kenc@suntan.viewlogic.com (Kenstir) (06/19/91)

I have a (char *) variable that I'm trying to protect
across a longjmp, so I want to tell GCC not to put it in
a register.  I believed that `volatile' did this, and it works
for integral types, but for pointer types, "gcc -O -Wall" still gives me

    foo.c:666: warning: variable `mbuf' may be clobbered by `longjmp'

It seems that GCC is assuming that `mbuf' is a pointer to a volatile
object, but that doesn't do me any good.  Can someone explain what
exactly the following two declarations mean and the difference between them?

    volatile int i;
    volatile char *mbuf;

I have looked at K&R 2, and also the GCC documentation.
Thanks very much for any light you can shed on the subject.

-- 
Kenneth H. Cox
Viewlogic Systems, Inc.
kenc@viewlogic.com
..!{harvard,husc6}!viewlogic.com!kenc

henry@zoo.toronto.edu (Henry Spencer) (06/19/91)

In article <1991Jun19.003124.28290@viewlogic.com> kenc@suntan.viewlogic.com (Kenstir) writes:
>I have a (char *) variable that I'm trying to protect
>across a longjmp...
>...
>    volatile int i;
>    volatile char *mbuf;

There is no fundamental difference between these declarations.  The first
takes the type "volatile int", applies no further type constructions to
it, and gives you a variable of that type.  The second takes the type
"volatile char", constructs a pointer to it, and gives you a variable
of that type.

To get a volatile pointer to char, say `char * volatile mbuf;'.  (Ugh.)

"Volatile" is part of the type; it's not like a storage class.
-- 
"We're thinking about upgrading from    | Henry Spencer @ U of Toronto Zoology
SunOS 4.1.1 to SunOS 3.5."              |  henry@zoo.toronto.edu  utzoo!henry

scs@adam.mit.edu (Steve Summit) (06/19/91)

In article <1991Jun19.003124.28290@viewlogic.com> kenc@suntan.viewlogic.com (Kenstir) writes:
>It seems that GCC is assuming that `mbuf' is a pointer to a volatile
>object, but that doesn't do me any good...
>    volatile char *mbuf;

Though perhaps not immediately obvious, the answer can be found
in the comp.lang.c frequently-asked questions list:

30.  What's the difference between "char const *p" and "char * const p"?

A:   "char const *p" is a pointer to a constant character (you can't
     change the character); "char * const p" is a constant pointer to a
     (variable) character (i.e. you can't change the pointer).  (Read
     these "inside out" to understand them.  See question 63.)

                                            Steve Summit
                                            scs@adam.mit.edu