简体   繁体   中英

Why the var and * to var gives diff values in this example of const_cast

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

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 .

For the following program

#include <iostream>

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

    std::cout << " value of x: " << x  << " addr of x "  << &x << '\n'
              << " and *p : "    << *p << " and addr p " << p  << '\n';
    return 0;
}

VC9 prints the same address for me. However:

  1. You are invoking undefined behavior , because you are not allowed to cast away const from an object if that object was a real const value. (OTOH, you are, for example, allowed to cast away const from a const reference if that reference refers to a non- const value.)
    In theory, when you invoke undefined behavior, according to the C++ standard your program might work as you expect, or it might not, or it might do so only on Sundays, or unless it's a holiday and full moon. But it might just as well format your HD, blow up your monitor, and make your girlfriend pregnant. According to the C++ standard, all this (and an infinite amount of other possibilities), are Ok.
    In practice, such a program might print out funny addresses.

  2. The compiler might completely optimize away all your code and just put in dummy values for printing. It shouldn't, however, do so for Debug builds. (Although that's a QoI issue, not a requirement.)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM