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.
Main Topics
Browse All TopicsI 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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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 !
|+|ayank
Business Accounts
Answer for Membership
by: AlexFMPosted on 2004-09-27 at 04:54:34ID: 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.