Velocity Reviews > C++ > Volatile

# Volatile

David Rasmussen
Guest
Posts: n/a

 06-07-2004
What is the difference in meaning and in consequences between

struct S
{
volatile A* a[10];
};

and

struct S
{
A* volatile a[10];
};

/David

Karl Heinz Buchegger
Guest
Posts: n/a

 06-07-2004
David Rasmussen wrote:
>
> What is the difference in meaning and in consequences between
>
> struct S
> {
> volatile A* a[10];
> };
>
> and
>
> struct S
> {
> A* volatile a[10];
> };
>

the same as with const.

First of all

A is a an array of 10 pointers.

in volatile A* it is the thing that the pointer
points to that is volatile

in A* volatile it is the pointer itself that is
volatile.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)

Bill Seurer
Guest
Posts: n/a

 06-07-2004
David Rasmussen wrote:

> What is the difference in meaning and in consequences between
>
> volatile A* a[10];
>
> and
>
> A* volatile a[10];

The first one is an array of (pointers to (volatile As)). It can also
be written this way: A volatile * a[10];

The second one is an array of (volatile pointers to (As)).

Most of the time the best way to decipher C++ declarations is to read
right to left.

Victor Bazarov
Guest
Posts: n/a

 06-07-2004
David Rasmussen wrote:
> What is the difference in meaning and in consequences between
>
> struct S
> {
> volatile A* a[10];
> };
>
> and
>
> struct S
> {
> A* volatile a[10];
> };
>
> /David

Just like with 'const', 'volatile' modifies the type of the variable
declared to its right. In the former case, (*a[10]) is the variable
(an array of ten pointers), so 'volatile' modifies the objects to
which the pointers point. In the latter case, 'volatile' relates to
'a', which means something like 'a' is a volatile array of ten pointers,
which means that every element (pointer) in that array is volatile.

The consequences... Not sure what you mean. Every object declared
'volatile' causes the compiler to emit code that makes sure the value
of the object is stored before the next sequence point. 'volatile'
is also a hint for the compiler to avoid aggressive optimisation.

Victor

Bill Seurer
Guest
Posts: n/a

 06-07-2004
Victor Bazarov wrote:

> Just like with 'const', 'volatile' modifies the type of the variable
> declared to its right.

This is NOT correct. Only in the case of a leading const or volatile
does it modify the thing to its right. Otherwise it is to the left.

Victor Bazarov
Guest
Posts: n/a

 06-07-2004
Bill Seurer wrote:
> Victor Bazarov wrote:
>
>> Just like with 'const', 'volatile' modifies the type of the variable
>> declared to its right.

>
>
> This is NOT correct. Only in the case of a leading const or volatile
> does it modify the thing to its right. Otherwise it is to the left.

I am not sure what you're talking about. Did you read what I wrote?
Besides, C++ does not have "thing" in its vocabulary.

Bill Seurer
Guest
Posts: n/a

 06-07-2004
Victor Bazarov wrote:

> Bill Seurer wrote:
>
>> Victor Bazarov wrote:
>>
>>> Just like with 'const', 'volatile' modifies the type of the variable
>>> declared to its right.

>>
>> This is NOT correct. Only in the case of a leading const or volatile
>> does it modify the thing to its right. Otherwise it is to the left.

>
> I am not sure what you're talking about. Did you read what I wrote?

Yes. You were wrong.

> Besides, C++ does not have "thing" in its vocabulary.

const and volatile can appear in many places with types, modifiers,
variable names, and probably other things on either side of them. In
fact, variables don't have to be involved at all:

typedef const int constint;

Use your statement to reconcile that

const int *a;
and
int const *a;

are the same.

Victor Bazarov
Guest
Posts: n/a

 06-07-2004
Bill Seurer wrote:
> Victor Bazarov wrote:
>
>> Bill Seurer wrote:
>>
>>> Victor Bazarov wrote:
>>>
>>>> Just like with 'const', 'volatile' modifies the type of the variable
>>>> declared to its right.
>>>
>>>
>>> This is NOT correct. Only in the case of a leading const or volatile
>>> does it modify the thing to its right. Otherwise it is to the left.

>>
>>
>> I am not sure what you're talking about. Did you read what I wrote?

>
>
> Yes. You were wrong.
>
>> Besides, C++ does not have "thing" in its vocabulary.

>
>
> const and volatile can appear in many places with types, modifiers,
> variable names, and probably other things on either side of them. In
> fact, variables don't have to be involved at all:
>
> typedef const int constint;

But 'constint' here has no effect on the program unless it is later
used in a declaration. When you use it in a declaration, a variable
will come into play (that means they do have to be involved).

>
> Use your statement to reconcile that
>
> const int *a;
> and
> int const *a;
>
> are the same.

'const' modifies the type of the object to its right (it's the object
who is to the right, not the type0. I used 'variable', perhaps that's
what you're picking at, I can't figure it out.

'const' in "const int *a" affects the '*a' (the object to the right of
the const), that is the 'int' to which 'a' points. 'const' in "int const
*a" affects the *a (again). In

const int k, *p; // or 'int const k, *p;'

both 'k' and '*p' are const.

Victor

Jeff Flinn
Guest
Posts: n/a

 06-07-2004

"Bill Seurer" <(E-Mail Removed)> wrote in message
news:ca22gk\$1jqq\$(E-Mail Removed)...
> Victor Bazarov wrote:
>
> > Bill Seurer wrote:
> >
> >> Victor Bazarov wrote:
> >>
> >>> Just like with 'const', 'volatile' modifies the type of the variable
> >>> declared to its right.
> >>
> >> This is NOT correct. Only in the case of a leading const or volatile
> >> does it modify the thing to its right. Otherwise it is to the left.

> >
> > I am not sure what you're talking about. Did you read what I wrote?

>
> Yes. You were wrong.

I don't think so.

> > Besides, C++ does not have "thing" in its vocabulary.

>
> const and volatile can appear in many places with types, modifiers,
> variable names, and probably other things on either side of them. In
> fact, variables don't have to be involved at all:
>
> typedef const int constint;
>
> Use your statement to reconcile that
>
> const int *a;
> and
> int const *a;
>
> are the same.

That is so, but that's not what the OP acked or to which Victor responded.
Which is of the form:

int * const var;

Jeff F