tags:

views:

107

answers:

4

Check this example for const_cast of int. I am using VC++ 2008 to compile this.

#include <iostream>
using namespace std;

void main() {
const int x=0;
int y=90;
int *p = const_cast<int *> (&x);
*p=y;

cout<<" value of x: "<<x<<" addr of x "<<&x<<endl
    <<" and *p : "<<*p<<" and addr p "<<p<<endl;

}

================

The output is

value of x: 0 addr of x 0012FF60
 and *p : 90 and addr p 0012FF60
A: 

maybe it was optimized by the compiler. he have all rights to do this. it is drawback of misusing const_cast. never use const_cast in such way.

yatagarasu
Never say never, except in this sentence.
Steven Sudit
To clarify, you're not completely wrong: there is definitely a problem with this usage. The reason I'm not upvoting is that your answer doesn't explain why it's wrong, and instead makes the same mistake I did, of considering it an optimization failure.
Steven Sudit
+1  A: 

The compiler is optimizing away the aliasing. Try it in debug mode, with optimizations disabled.

edit

The compiler is optimizing away the aliasing, yes, but it's not an error in the optimization. Rather, the code is doing something undefined, which is leading to an unwanted but legal behavior. Staffan's answer clarifies this.

As the FAQ said, in almost all cases where you're tempted to use const_cast, you should be using mutable instead. If, as in the sample code here, you can't use mutable, this is an indication that something might be wrong.

Steven Sudit
Tried that - I still get the same behaviour. It's not compiler optimisation, at least not on mine (VC++ 2008)
Ragster
What Staffan said is more accurate: A local constant is treated by the compiler as pure syntactic sugar. I suspect that if you hadn't taken the address of x, the compiler would never have bothered allocating any memory for it. Instead, it would have passed it as a literal value at the assembler level. Even though it did allocate that int, it didn't bother looking in that location when it came time to use the value. In short, your code is trying to do something that is, at the very least, a very bad idea that should almost always be avoided.
Steven Sudit
To chk if Compiler Optimization is the cause of this, I tried making x to be the volatile const int x=0; now accessing it gives the same value as pointer....
saurabh
Even if this "fixes" the problem, I can't recommend it because you're counting on undefined behavior. It could just as easily fail in another version of the compiler, not to mention any other compiler.
Steven Sudit
+5  A: 

You should not const_cast a variable defined as const. I don't have the standard at hand but I'm fairly certain it defines such an operation as resulting in undefined behaviour.

For an example of why this results in undefined behaviour consider an MCU where things defined as const are stored in non-volatile memory (flash, EEPROM or something even less volatile).

There's more to read in the C++ FAQ Lite.

Staffan
Right, the use case that const_cast is made for involves a non-const instance that is automatically cast to const when calling a function.
Steven Sudit
+2  A: 
sbi
I believe the C++0x standard specifies that pregnancy is not an allowable side-effect.
Steven Sudit
@Steven: TTBOMK, you're wrong. The standard does not spell out _any_ restrictions for UB. (Hence the proverbial _nasal demons_.)
sbi
@sbi: My sources tell me that Stroustrup put his foot down on this issue after having to subject himself to an exceptional abortion.
Steven Sudit
@Steven; ISTR him having a son and a daughter. ICBWT.
sbi
@sbi: That is correct (see http://www2.research.att.com/~bs/bio.html). However, he is their father, not mother.
Steven Sudit
@Steven: nevertheless, unless you supply chapter and verse, I believe you are mistaken about this. `:)`
sbi
@sbi: I'm glad you asked. Here's your chapter and verse: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1727.pdf
Steven Sudit
@Steven: Are you pulling my leg? That says nothing at all about casting away `const`.
sbi
@sbi: The title is "Changing Undefined Behavior into Diagnosable Errors", which is to say that it's about fixing the problem of unwanted pregnancies, nasal demons and other possible effects of doing such things as casting away const.
Steven Sudit
@Steven: Well, that explains our misunderstanding then. I was looking at what saurabh actually did (illegally casting aways `const`) and you went by the questions title...
sbi
@sbi: Well, I went by the point of the article, which is that some effort is being made to limit the amount of undefined behavior in the language.
Steven Sudit