const correctness

A

Alvin

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?
 
V

Victor Bazarov

Alvin said:
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
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Alvin said:
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
 
A

Alvin

Victor said:
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!
 
I

Ian Collins

Alvin said:
Victor Bazarov wrote:




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.
 
M

Martin Eisenberg

Alvin said:
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
 
D

Duane Hebert

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top