const correctness

Discussion in 'C++' started by Alvin, Jan 20, 2006.

  1. Alvin

    Alvin Guest

    Hello,

    I have a static library that I created. I've been updating the source to be
    const-correct as according to the C++ FAQ Lite section 18
    (http://www.parashift.com/c -faq-lite/const-correctness.html). I know I
    should have done this in the first place, but better late than never. ;)

    I have been declaring inspect functions const as in:
    int inspect() const;

    and if a parameter is not going to be altered as in:
    int foo(const MyObject *obj) const;
    int bar(const MyObject *obj);

    I have been systematically testing the library with a backup copy to ensure
    I'm not introducing any *new* bugs. So, functionally, the const-correct
    version works just as well as the original.

    As I have been doing this, I have noticed that the size of the library has
    slowly been increasing. Not by much, just getting larger by a couple of
    kilobytes. Does anyone know why this would occur?

    --
    Alvin
    SUSE 9.3
    GCC 3.3.5
    Kernel 2.6.11.4-21.10
     
    Alvin, Jan 20, 2006
    #1
    1. Advertising

  2. Alvin wrote:
    > I have a static library that I created. I've been updating the source to be
    > const-correct as according to the C++ FAQ Lite section 18
    > (http://www.parashift.com/c -faq-lite/const-correctness.html). I know I
    > should have done this in the first place, but better late than never. ;)
    >
    > I have been declaring inspect functions const as in:
    > int inspect() const;
    >
    > and if a parameter is not going to be altered as in:
    > int foo(const MyObject *obj) const;
    > int bar(const MyObject *obj);
    >
    > I have been systematically testing the library with a backup copy to ensure
    > I'm not introducing any *new* bugs. So, functionally, the const-correct
    > version works just as well as the original.
    >
    > As I have been doing this, I have noticed that the size of the library has
    > slowly been increasing. Not by much, just getting larger by a couple of
    > kilobytes. Does anyone know why this would occur?
    >


    The symbols' internal (mangled) names in the library get longer, maybe?

    There is no certain way to answer your question in terms of C++ language,
    perhaps ask about this in the newsgroup that deals with your compiler...

    V
     
    Victor Bazarov, Jan 20, 2006
    #2
    1. Advertising

  3. Alvin wrote:
    > Hello,
    >
    > I have a static library that I created. I've been updating the source to be
    > const-correct as according to the C++ FAQ Lite section 18
    > (http://www.parashift.com/c -faq-lite/const-correctness.html). I know I
    > should have done this in the first place, but better late than never. ;)
    >
    > I have been declaring inspect functions const as in:
    > int inspect() const;
    >
    > and if a parameter is not going to be altered as in:
    > int foo(const MyObject *obj) const;
    > int bar(const MyObject *obj);
    >
    > I have been systematically testing the library with a backup copy to ensure
    > I'm not introducing any *new* bugs. So, functionally, the const-correct
    > version works just as well as the original.
    >
    > As I have been doing this, I have noticed that the size of the library has
    > slowly been increasing. Not by much, just getting larger by a couple of
    > kilobytes. Does anyone know why this would occur?
    >
    > --
    > Alvin
    > SUSE 9.3
    > GCC 3.3.5
    > Kernel 2.6.11.4-21.10


    New compiler version? Different compiler options?
    "Duplicate" functions where the non-const version is still needed?

    Stephan
     
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Jan 20, 2006
    #3
  4. Alvin

    Alvin Guest

    Victor Bazarov wrote:

    >> As I have been doing this, I have noticed that the size of the library
    >> has slowly been increasing. Not by much, just getting larger by a couple
    >> of kilobytes. Does anyone know why this would occur?
    >>

    >
    > The symbols' internal (mangled) names in the library get longer, maybe?


    I would say that this is the reason. I used the 'nm' command and compared
    the output of the two object files (one that is const-correct and one
    without).

    Looks like the const-correct object file adds a 'K' to the mangled names
    where const is specified (parameter and function declaration).

    > There is no certain way to answer your question in terms of C++ language,
    > perhaps ask about this in the newsgroup that deals with your compiler...


    Seems that you are right, this is a compiler issue. I was just surprised
    when I saw this as I thought using 'const' only effected compile time (sort
    of like syntactic decoration/hinting). In fact, I assumed (which is
    dangerous I know) that the compiler would actually make the binary smaller
    cause it has been told that certain functions and variables cannot be
    mutated.

    Regardless, the small increase in size is worth it when compared to the
    potential problems that can crop-up without using const.

    Thanks!

    --
    Alvin
    SUSE 9.3
    GCC 3.3.5
    Kernel 2.6.11.4-21.10
     
    Alvin, Jan 20, 2006
    #4
  5. Alvin

    Ian Collins Guest

    Alvin wrote:
    > Victor Bazarov wrote:
    >
    >
    >>>As I have been doing this, I have noticed that the size of the library
    >>>has slowly been increasing. Not by much, just getting larger by a couple
    >>>of kilobytes. Does anyone know why this would occur?
    >>>

    >>
    >>The symbols' internal (mangled) names in the library get longer, maybe?

    >
    >
    > I would say that this is the reason. I used the 'nm' command and compared
    > the output of the two object files (one that is const-correct and one
    > without).
    >

    Try stripping your binary and compare.

    --
    Ian Collins.
     
    Ian Collins, Jan 20, 2006
    #5
  6. Alvin wrote:

    > I have a static library that I created. I've been updating the
    > source to be const-correct as according to the C++ FAQ Lite
    > section 18
    > (http://www.parashift.com/c -faq-lite/const-correctness.html).


    > As I have been doing this, I have noticed that the size of the
    > library has slowly been increasing. Not by much, just getting
    > larger by a couple of kilobytes. Does anyone know why this would
    > occur?


    As a guess, the const qualifications are lengthening your functions'
    mangled names. Try and see if a test program compiled with each
    library version and linked with symbol stripping on results in the
    same executable size.


    Martin

    --
    Quidquid latine scriptum sit, altum viditur.
     
    Martin Eisenberg, Jan 20, 2006
    #6
  7. Alvin

    Duane Hebert Guest

    "Victor Bazarov" <> wrote in message
    news:O_9Af.69$01.us.to.verio.net...

    > The symbols' internal (mangled) names in the library get longer, maybe?
    >
    > There is no certain way to answer your question in terms of C++ language,
    > perhaps ask about this in the newsgroup that deals with your compiler...


    I've seen something similar with Borland's compiler. When tagging a
    function as const, the compiler can possibly try to inline it where it
    may not have without the const. At least with the Borland compiler,
    this increases the exe size.
     
    Duane Hebert, Jan 20, 2006
    #7
    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. Jim Strathmeyer

    stl list, const correctness

    Jim Strathmeyer, Mar 19, 2005, in forum: C++
    Replies:
    2
    Views:
    515
    Pete Becker
    Mar 20, 2005
  2. Matthias Kaeppler

    const-correctness and lambda expression

    Matthias Kaeppler, Apr 16, 2005, in forum: C++
    Replies:
    1
    Views:
    616
    Kanenas
    Apr 20, 2005
  3. Replies:
    10
    Views:
    578
    mlimber
    Oct 5, 2005
  4. Javier
    Replies:
    2
    Views:
    574
    James Kanze
    Sep 4, 2007
  5. fungus
    Replies:
    13
    Views:
    893
    fungus
    Oct 31, 2008
Loading...

Share This Page