HOWTO: const and pointer variants in C and C++

Discussion in 'C++' started by Adem, Nov 11, 2008.

  1. Adem

    Adem Guest

    HOWTO: const and pointer variants in C and C++ :

    void test()
    {
    int n = 1; // n is non-const data of type int
    ++n; // ok

    const int c = 2; // c is const data of type int
    // ++c; // err

    // Case1:
    int* p1 = &n; // p1 is a non-const ptr to non-const data
    ++p1; // ok
    *p1 += 3; // ok

    // int* px = &c; // err

    // Case2:
    int* const p2 = &n; // p2 is a const ptr to non-const data
    // ++p2; // err
    *p2 += 3; // ok

    // Case3:
    const int* p3 = &c; // p3 is a non-const ptr to const data
    ++p3; // ok
    // *p3 += 3; // err

    // Case4:
    const int* const p4 = &c; // p4 is a const ptr to const data
    // ++p4; // err
    // *p4 += 3; // err

    // Case5:
    const int* const p5 = &n; // Similar to Case4 but here non-const n is treated as const data when accessed via p5
    // ++p5; // err
    // *p5 += 3; // err

    // Case6:
    const int *const p6 = &c; // same as Case4
    // ++p6; // err
    // *p6 += 3; // err

    // Case7:
    const int *const p7 = &n; // Same as Case5
    // ++p7; // err
    // *p7 += 3; // err

    // const* int p8 = &n; // err
    // const* int p9 = &c; // err

    // const int const* pa = &n; // err: duplicate 'const'
    // const int const* pb = &c; // err: duplicate 'const'
    }

    For safety and speed you should use const whenever possible.
     
    Adem, Nov 11, 2008
    #1
    1. Advertising

  2. blargg wrote:
    > wrote:
    >> blargg wrote:
    >>> Care to show any examples of where const helps speed-wise? That is, a
    >>> program where removing const wouldn't change the semantics of the program,
    >>> but would reduce its speed. I'm not arguing against using const, just
    >>> questioning this often-made claim.

    >> const could help a compiler determine if a variable
    >> could be replaced with a constant in machine code.
    >> Opcodes with operands, usually have smaller faster
    >> versions which operate on constants.

    > [...]
    >
    > Yes, but can you provide EXAMPLES of where const actually helps the
    > compiler determine this, where it couldn't determine it without const?


    'const' in C++ has serves multiple purposes. It can 1) declare constness
    of an object itself, and 2) declare constness of an access path to an
    object, 3) serve as a distinguishing trait in overload resolution.

    How the third role can be used in "optimization" is rather obvious. But
    these are basically user optimizations, as opposed to complier
    optimizations. I assume that you are talking about compiler
    optimizations, to which the third role is not immediately relevant.

    I don't think I need to provide examples of how the first role of
    'const' can speed up the code. They are immediately obvious as well.
    Constant values can be calculated at compile time. Code that depends on
    constant values can be reduced at compile time. Etc.

    The second role of 'const' is more about discipline, than about
    optimization. In general case, the constness of access path does not
    imply the constness of the object this access path leads to. Which means
    that in general case without preforming a thorough aliasing analysis,
    the compiler can't do much based on the constness of access path itself.
    And if the compiler is smart enough to perform a sufficiently thorough
    aliasing analysis, it should be smart enough to optimize it regardless
    of whether the explicit 'const' is supplied by the user or not. So, I'd
    agree that the second role of 'const' by itself is not immediately
    useful for compiler optimizations. However, it is worth noting that it
    might become quite useful if the user explicitly gives the compiler the
    permission to perform "overly agressive" optimizations (meaning, that
    they might be formally invalid in general case) by assuming some
    aliasing properties of the code, instead of trying to derive them from
    the code itself. (I'm not sure, but it is quite possible that the
    combination of 'const' with C99's 'restrict' allows for more valid
    straightforward optimizations that just a mere 'restrict'.)

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 11, 2008
    #2
    1. Advertising

  3. Adem

    James Kuyper Guest

    pete wrote:
    > Hendrik Schober wrote:
    >> pete wrote:
    >>> [...]
    >>> I don't think that removing const
    >>> ever changes the semantics of a program.

    >>
    >> struct test {
    >> int f() {return 1;}
    >> int f() const {return 0;}
    >> };
    >>
    >> int main()
    >> {
    >> /*const*/ test x;
    >> return x.f();
    >> }

    >
    > In order to make a point,
    > you would have to post a correct c program


    In case you haven't noticed, this message is cross-posted to two groups
    where C++ code is entirely appropriate; the Subject: line might also be
    a hint.

    > which contains the const keyword,
    > and which also does something different when const is removed.


    The above program does something quite different when the comment
    markers are removed from around the second 'const'.
     
    James Kuyper, Nov 11, 2008
    #3
  4. Adem

    Flash Gordon Guest

    Hendrik Schober wrote, On 11/11/08 15:41:
    > pete wrote:
    >> [...]
    >> I don't think that removing const
    >> ever changes the semantics of a program.

    >
    > struct test {
    > int f() {return 1;}
    > int f() const {return 0;}
    > };


    This is not valid C and is an example of why cross-posting between C and
    C++ groups is generally a bad idea. Please keep answers which are
    specifically C++ in groups where C++ is topical.
    --
    Flash Gordon
    If spamming me sent it to
    If emailing me use my reply-to address
    See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
     
    Flash Gordon, Nov 11, 2008
    #4
  5. Adem

    Flash Gordon Guest

    James Kuyper wrote, On 11/11/08 19:12:
    > pete wrote:


    <snip>

    >> In order to make a point,
    >> you would have to post a correct c program

    >
    > In case you haven't noticed, this message is cross-posted to two groups
    > where C++ code is entirely appropriate; the Subject: line might also be
    > a hint.


    It is also posted to a group where C++ is NOT topical. The way pete
    indicated this might not be the best, but it is certainly true that the
    example given was not correct for comp.lang.c where only C is topical.
    Is it really too difficult for people with C++ specific answers to drop
    the cross-post to comp.lang.c?
    --
    Flash Gordon
    If spamming me sent it to
    If emailing me use my reply-to address
    See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
     
    Flash Gordon, Nov 11, 2008
    #5
  6. Flash Gordon wrote:
    >
    > Is it really too difficult for people with C++ specific answers to drop
    > the cross-post to comp.lang.c?


    Dropping is not difficult. _Noticing_ might be. I, for one, use
    Thunderbird and most of the time don't notice that I post to multiple
    newsgroups at once.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 11, 2008
    #6
  7. Adem

    Flash Gordon Guest

    Pete Becker wrote, On 11/11/08 20:24:
    > On 2008-11-11 15:16:00 -0500, Flash Gordon <> said:
    >
    >> James Kuyper wrote, On 11/11/08 19:12:
    >>> pete wrote:

    >>
    >> <snip>
    >>
    >>>> In order to make a point,
    >>>> you would have to post a correct c program
    >>>
    >>> In case you haven't noticed, this message is cross-posted to two groups
    >>> where C++ code is entirely appropriate; the Subject: line might also be
    >>> a hint.

    >>
    >> It is also posted to a group where C++ is NOT topical. The way pete
    >> indicated this might not be the best, but it is certainly true that the
    >> example given was not correct for comp.lang.c where only C is topical.
    >> Is it really too difficult for people with C++ specific answers to drop
    >> the cross-post to comp.lang.c?

    >
    > It's no more difficult than for people with comments about the
    > appropriateness of postings in comp.lang.c to drop the cross-post to
    > comp.lang.c++.


    I had not registered that it was James Kuyper, who does read
    comp.lang.c, who made the comment above about it being topical in two of
    the groups this is cross-posted to. Sorry. If I had registered I would
    have dropped the cross-post.

    In general where threads are cross-posted between comp.lang.c and
    comp.lang.c++ it is people who don't read comp.lang.c who post messages
    cross-posted to it which are not topical "here", so in general making
    such a comment just in comp.lang.c can't do any good. People in all
    three of these groups (including comp.lang.c) need to watch out for
    cross-posts and drop comp.ang.c++ where it is a C specific answer, and
    comp.lang.c where it is a C++ answer, and tell the OP what a bad idea
    the cross-post is because of the problems it causes.
    --
    Flash Gordon
    If spamming me sent it to
    If emailing me use my reply-to address
    See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/
     
    Flash Gordon, Nov 11, 2008
    #7
  8. On 11 Nov 2008 at 21:20, Flash Gordon wrote:
    > People in all three of these groups (including comp.lang.c) need to
    > watch out for cross-posts and drop comp.ang.c++ where it is a C
    > specific answer, and comp.lang.c where it is a C++ answer,


    You might /like/ people to do this for your own political reasons. But
    they don't /need/ to. They can do whatever they think is appropriate,
    whether it suits your prejudices or not.

    > and tell the OP what a bad idea the cross-post is because of the
    > problems it causes.


    There is no problem - you just choose to believe there is for your own
    political reasons.
     
    Antoninus Twink, Nov 11, 2008
    #8
  9. Adem

    James Kuyper Guest

    Flash Gordon wrote:
    > Pete Becker wrote, On 11/11/08 20:24:
    >> On 2008-11-11 15:16:00 -0500, Flash Gordon <> said:
    >>
    >>> James Kuyper wrote, On 11/11/08 19:12:
    >>>> pete wrote:
    >>> <snip>
    >>>
    >>>>> In order to make a point,
    >>>>> you would have to post a correct c program
    >>>> In case you haven't noticed, this message is cross-posted to two groups
    >>>> where C++ code is entirely appropriate; the Subject: line might also be
    >>>> a hint.
    >>> It is also posted to a group where C++ is NOT topical. The way pete
    >>> indicated this might not be the best, but it is certainly true that the
    >>> example given was not correct for comp.lang.c where only C is topical.
    >>> Is it really too difficult for people with C++ specific answers to drop
    >>> the cross-post to comp.lang.c?

    >> It's no more difficult than for people with comments about the
    >> appropriateness of postings in comp.lang.c to drop the cross-post to
    >> comp.lang.c++.

    >
    > I had not registered that it was James Kuyper, who does read
    > comp.lang.c, who made the comment above about it being topical in two of
    > the groups this is cross-posted to. Sorry. If I had registered I would
    > have dropped the cross-post.


    I think that a comparison between C and C++ with regard to 'const' and
    pointers should be on-topic in all three groups, but that everyone
    involved in such a discussion should be careful to distinguish which
    language they're talking about at any given point in the discussion,
    except when making a statement that they believe to be true in both
    languages.
     
    James Kuyper, Nov 12, 2008
    #9
  10. Adem

    James Kanze Guest

    On Nov 11, 4:34 pm, (blargg) wrote:
    > wrote:
    > > blargg wrote:
    > > > Care to show any examples of where const helps speed-wise?
    > > > That is, a program where removing const wouldn't change
    > > > the semantics of the program, but would reduce its speed.
    > > > I'm not arguing against using const, just questioning this
    > > > often-made claim.


    > > const could help a compiler determine if a variable could be
    > > replaced with a constant in machine code. Opcodes with
    > > operands, usually have smaller faster versions which operate
    > > on constants.


    > [...]


    > Yes, but can you provide EXAMPLES of where const actually
    > helps the compiler determine this, where it couldn't determine
    > it without const?


    It's generally true of top-level const. Something like:

    int const i = 42 ;

    , for example. The compiler knows that regardless of what
    happens elsewhere in a legal program, i will always be equal to
    42.

    > About the only one is a non-static variable at file scope,
    > where the compiler can't in general assume its value after
    > code in other translation units has executed, unless it's
    > declared const. A variantis a local const object whose address
    > is passed to a function; removing const would prevent the
    > compiler from assuming its value doesn't change during the
    > function call. I contend that these are rare cases.


    In C, probably, since such variables can't be used in constant
    expressions. In C++, it's actually quite common.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Nov 12, 2008
    #10
  11. Adem

    Old Wolf Guest

    On Nov 11, 11:19 pm, "Adem" <> wrote:
    >     const  int*       p3 = &c;  // p3 is a non-const ptr to const data


    Not exactly, e.g. :
    const int *p3 = &n; // not error


    Also, you omitted:
    int const *p
    int const *const p
     
    Old Wolf, Nov 12, 2008
    #11
    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. coala
    Replies:
    3
    Views:
    379
    coala
    Sep 6, 2006
  2. coala
    Replies:
    1
    Views:
    596
    Victor Bazarov
    Sep 6, 2006
  3. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
  4. Adem
    Replies:
    10
    Views:
    485
    Old Wolf
    Nov 12, 2008
  5. Disc Magnet
    Replies:
    1
    Views:
    633
    Ian Collins
    May 6, 2010
Loading...

Share This Page