Solved

const in C++

Posted on 2004-09-27
9
781 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: 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!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

719 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