• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 786
  • Last Modified:

const in C++

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.
1 Solution
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.

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.

Again I emphasize on my words "compile time" binding of variable name (x) with hard coded (const) value.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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


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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now