Volatile

  • Thread starter David Rasmussen
  • Start date
D

David Rasmussen

What is the difference in meaning and in consequences between

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

and

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

/David
 
K

Karl Heinz Buchegger

David said:
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.
 
B

Bill Seurer

David said:
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.
 
V

Victor Bazarov

David said:
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
 
B

Bill Seurer

Victor said:
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.
 
V

Victor Bazarov

Bill said:
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.
 
B

Bill Seurer

Victor said:
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.
 
V

Victor Bazarov

Bill said:
Yes. You were wrong.



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
 
J

Jeff Flinn

Bill Seurer said:
Yes. You were wrong.

I don't think so.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top