简体   繁体   English

为什么在const_cast的示例中var和* to var给出差异值

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

Check this example for const_cast of int. 在此示例中检查int的const_cast。 I am using VC++ 2008 to compile this. 我正在使用VC ++ 2008进行编译。

#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. 您不应该const_cast定义为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). 有关导致未定义行为的原因的示例,请考虑使用MCU,其中定义为const的内容存储在非易失性存储器(闪存,EEPROM或什至不易挥发的内容)中。

There's more to read in the C++ FAQ Lite . 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. VC9为我打印相同的地址。 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. 您正在调用未定义的行为 ,因为如果该对象是真正的const值,则不允许该对象抛弃const (OTOH, you are, for example, allowed to cast away const from a const reference if that reference refers to a non- const value.) (OTOH,你是,例如,允许抛弃constconst参考如果参照是指非const值)。
    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. 从理论上讲,当您调用未定义的行为时,根据C ++标准,您的程序可能会按预期运行,或者可能无法运行,或者可能仅在星期日运行,或者除非是假日和满月。 But it might just as well format your HD, blow up your monitor, and make your girlfriend pregnant. 但这也可能会格式化HD,炸毁显示器并让女友怀孕。 According to the C++ standard, all this (and an infinite amount of other possibilities), are Ok. 根据C ++标准,所有这些(以及无数其他可能性)都是可以的。
    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.) (尽管这是一个QoI问题,而不是必需的。)

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. Staffan的答案澄清了这一点。

As the FAQ said, in almost all cases where you're tempted to use const_cast , you should be using mutable instead. 就像FAQ所说,在几乎所有试图使用const_cast情况下,您都应该使用mutable If, as in the sample code here, you can't use mutable , this is an indication that something might be wrong. 如此处的示例代码中所示,如果您不能使用mutable ,则表明可能有问题。

maybe it was optimized by the compiler. 也许它是由编译器优化的。 he have all rights to do this. 他有权这样做。 it is drawback of misusing const_cast. 这是滥用const_cast的缺点。 never use const_cast in such way. 切勿以这种方式使用const_cast。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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