volatile const int foo

Discussion in 'C Programming' started by Mantorok Redgormor, Feb 16, 2004.

  1. strictly speaking we can't say that const
    means "read only" but instead that it means
    "do not modify me please" since an object
    can be both modifiable and not modifiable
    or volatile and const, right?

    and if an implementation chose to put
    things that are const in read only memory
    if that const thing was also volatile
    then in that case, it would not be in
    read only memory, correct?



    --
    nethlek
     
    Mantorok Redgormor, Feb 16, 2004
    #1
    1. Advertising

  2. (Mantorok Redgormor) writes:

    > strictly speaking we can't say that const means "read only" but
    > instead that it means "do not modify me please" since an object can be
    > both modifiable and not modifiable or volatile and const, right?


    Not sure what you mean by that. If you have something like

    const int i = 5;

    then every attempt to modify `i' is an error. OTOH, if you have

    int i = 5;
    const int *ip = &i;

    you can modify `i' directly, but it's an error to modify it through `ip'
    (e.g. by assigning to `*ip').

    > and if an implementation chose to put things that are const in read
    > only memory if that const thing was also volatile then in that case,
    > it would not be in read only memory, correct?


    Not necessarily. If you have

    const volatile int i = 5;

    the implementation could still put `i' in read only memory. Perhaps a
    better example is

    const volatile int *ip = some_system_specific_initialization ();

    It would still be an error to modify `*ip' in any way, but the
    implementation would have to assume that the object pointed to can be
    changed by something beyond its control. Imagine, for example, that
    `some_system_specific_initialization ()' returns the address of a
    hardware timer, which is incremented regularly by the hardware, but
    cannot be changed by the C program.

    Martin
     
    Martin Dickopp, Feb 16, 2004
    #2
    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. Gunnar

    int foo and int foo()

    Gunnar, Jan 19, 2004, in forum: C++
    Replies:
    8
    Views:
    391
    Robert Paul Clark
    Jan 22, 2004
  2. Replies:
    3
    Views:
    399
    John Roth
    Jul 29, 2005
  3. C. J. Clegg

    const int FOO vs #define FOO

    C. J. Clegg, Feb 20, 2006, in forum: C++
    Replies:
    9
    Views:
    379
    Jack Klein
    Feb 21, 2006
  4. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    414
    JaredGrubb
    Sep 24, 2008
  5. Ralf Goertz

    const foo * and const foo &

    Ralf Goertz, Jan 26, 2011, in forum: C++
    Replies:
    3
    Views:
    535
    Ralf Goertz
    Jan 27, 2011
Loading...

Share This Page