Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > const correctness

Reply
Thread Tools

const correctness

 
 
Alvin
Guest
Posts: n/a
 
      01-20-2006
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-lit...rectness.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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-20-2006
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-lit...rectness.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
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      01-20-2006
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-lit...rectness.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

 
Reply With Quote
 
Alvin
Guest
Posts: n/a
 
      01-20-2006
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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      01-20-2006
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.
 
Reply With Quote
 
Martin Eisenberg
Guest
Posts: n/a
 
      01-20-2006
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-lit...rectness.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.
 
Reply With Quote
 
Duane Hebert
Guest
Posts: n/a
 
      01-20-2006

"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:O_9Af.69$(E-Mail Removed)01.us.to.veri o.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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Const correctness for pointer data members quantdev2004@yahoo.co.uk C++ 10 10-05-2005 12:09 PM
const-correctness and lambda expression Matthias Kaeppler C++ 1 04-20-2005 10:09 PM
stl list, const correctness Jim Strathmeyer C++ 2 03-20-2005 12:37 AM



Advertisments