Volatile

Discussion in 'C++' started by David Rasmussen, Jun 7, 2004.

  1. What is the difference in meaning and in consequences between

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

    and

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

    /David
     
    David Rasmussen, Jun 7, 2004
    #1
    1. Advertising

  2. 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
     
    Karl Heinz Buchegger, Jun 7, 2004
    #2
    1. Advertising

  3. David Rasmussen

    Bill Seurer Guest

    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.
     
    Bill Seurer, Jun 7, 2004
    #3
  4. 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
     
    Victor Bazarov, Jun 7, 2004
    #4
  5. David Rasmussen

    Bill Seurer Guest

    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.
     
    Bill Seurer, Jun 7, 2004
    #5
  6. 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.
     
    Victor Bazarov, Jun 7, 2004
    #6
  7. David Rasmussen

    Bill Seurer Guest

    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.
     
    Bill Seurer, Jun 7, 2004
    #7
  8. 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
     
    Victor Bazarov, Jun 7, 2004
    #8
  9. David Rasmussen

    Jeff Flinn Guest

    "Bill Seurer" <> wrote in message
    news:ca22gk$1jqq$...
    > 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
     
    Jeff Flinn, Jun 7, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Knute Johnson

    Re: Volatile?

    Knute Johnson, Jul 1, 2003, in forum: Java
    Replies:
    17
    Views:
    1,460
    Knute Johnson
    Jul 3, 2003
  2. Doug Pardee

    Re: Volatile?

    Doug Pardee, Jul 1, 2003, in forum: Java
    Replies:
    0
    Views:
    901
    Doug Pardee
    Jul 1, 2003
  3. Replies:
    1
    Views:
    923
    Knute Johnson
    Aug 29, 2003
  4. Daniel

    Can volatile be trusted?

    Daniel, Sep 11, 2003, in forum: Java
    Replies:
    7
    Views:
    478
    Chris Uppal
    Sep 12, 2003
  5. ben
    Replies:
    5
    Views:
    599
    Ulrich Eckhardt
    Jan 11, 2005
Loading...

Share This Page