Solved

const in C++

Posted on 2004-09-27
9
778 Views
Last Modified: 2012-08-13
I am trying to understand the exact semantics of const in C++. So I wrote the following program:

#include <iostream>

using namespace std;

int main() {
    const int x = 2;
    int *p = (int *)&x;
    *p = 1;
    cout << x << endl;
}

My expectation was that the program should print 1 but it prints 2. Is this correct behavior according to the C++ standard? Surely when the programmer has explicitly overridden the constness of a variable, the compiler must obey. But this does not seem to be the case. Are there any explanations? I am using "g++ -g test.cpp" to compile the program.
0
Comment
Question by:priyendra
9 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 12159210
Why does this happen? This is Assembly code produced by C++ compiler:

13:       const int x = 2;
00401798   mov         dword ptr [ebp-4],2
14:       int *p = (int *)&x;
0040179F   lea         eax,[ebp-4]
004017A2   mov         dword ptr [ebp-8],eax
15:       *p = 1;
004017A5   mov         ecx,dword ptr [ebp-8]
004017A8   mov         dword ptr [ecx],1
16:       cout << x << endl;
004017AE   push        offset @ILT+195(std::endl) (004010c8)
004017B3   push        2
...

push 2 - compiler passes hard-coded 2 instead of c to cout. I tested with VC++ 6.0 and 7.1 with the same result. Since VC++ 7.1 is almost 100% standard compliant, I guess this is correct behaviour.
Compiler doesn't show error message or warning because it cannot recognize constness violation done using pointer.
0
 
LVL 5

Expert Comment

by:info_expert
ID: 12160039
Hi,

Because the association of a const variable is with the name of the variable and not the address of variable, at compile time.

So the no matter you try to modify it by other methods, it will remain same.

Regards.
0
 
LVL 5

Expert Comment

by:info_expert
ID: 12160051
Again I emphasize on my words "compile time" binding of variable name (x) with hard coded (const) value.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 48

Expert Comment

by:AlexFM
ID: 12160084
Debugger shows that after the line:
*p = 1;
x value is changed to 1. However, compiler doesn't use this value and replaces it with hard-coded initial value 2:
cout << 2 << endl;          // in Assembly code
0
 

Expert Comment

by:s_senthil_kumar
ID: 12161120
That's because you've used a C style cast, which doesn't care about const-ness. Try it with a static cast.
int main() {
    const int x = 2;
    int *p = static_cast<int *>(&x);
    *p = 1;
    cout << x << endl;

     return 1;
}

The compiler will now say that it can't cast from const int * to int *. Of course, you can override that with a const_cast, but that's beside the point.  You can cast to const int *, but then the compiler will disallow *p = 1

Moral of the story : Never use C style casts unless they're absolutely necessary.
0
 

Expert Comment

by:bhattmayank
ID: 12161587
Yes "s_senthil_kumar" is right, try it out what he said. and u will find why it was like this.
even in the symbol tables i guess the X will have the value 2 and though as per the assembly shown above it's just reading the symbol table and try to print the associated value of x in symbole table !
|+|ayank
0
 
LVL 4

Expert Comment

by:Farshid-Zaker
ID: 12162837
Const is an early binding element. It means it is replaced by its value at compile time. & and * operators are simulated on const elements at compile time and they return virtual values, not really the address of a const variable!
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12167040
Hi priyendra,

A const does have an address which you used - such as for passing const argument by reference.

but since the compiler knows this is a constant, it assumes it can use the value directly.

Cheers!

Stefan
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12174075
>> but since the compiler knows this is a constant, it assumes it can use the value directly
true. should try with const members in classes and see what the compiler does.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question