使用const_cast的奇怪行为 [英] Weird Behaviour with const_cast

查看:92
本文介绍了使用const_cast的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道使用 const_cast 通常是坏主意,但我玩弄它,我遇到了一个奇怪的行为,其中:



两个指针具有相同的地址值,但在取消引用时,给出不同的数据值。



有没有人对此有解释?



代码

  #include< ; iostream> 

int main()
{
const int M = 10;

int * MPtr = const_cast< int *>(& M);

(* MPtr)++;

std :: cout<< MPtr =< MPtr < (* MPtr)=< (* MPtr)< std :: endl;
std :: cout<< & M =<< & M < M =<< M < std :: endl;
}

输出

  MPtr = 0x7fff9b4b6ce0(* MPtr)= 11 
& M = 0x7fff9b4b6ce0 M = 10
pre>

解决方案

因此,除了它的未定义的行为(它是),编译器完全可以使用事实上 M 是常数,因此不会改变,在 cout ...< M < ... ,因此可以使用具有立即值10的指令,而不是存储在 M 的内存中的实际值。 (当然,标准不会说明这是如何工作的,超过它的未定义,编译器能够在不同的情况下选择不同的解决方案等,所以完全有可能你会得到不同的结果,如果你修改代码,使用不同的编译器,不同版本的编译器或风吹在不同的方向)。



未定义行为的部分棘手之处在于它包含完全符合您的期望以及几乎可以期望的 。编译器也可以决定启动tetris,如果它发现这是你在做什么。



是的,这是为什么你不应该使用 const_cast 的原因之一。至少不是原来 const 的东西 - 如果你有这些行的东西没关系:

  int x; 

void func(const int * p)
{
...
int * q = const_cast< int *>(p);

* q = 7;
}


...

func(& x);

在这种情况下, x const,当我们将它传递给 func 时,它变成const。当然,编译器可能仍然假设 x func 中没有更改,因此您可能有问题。 ..


I know that using const_cast is generally bad idea, but I was playing around with it and I came across a weird behaviour, where:

Two pointers have the same address value, yet when de-referenced, give different data values.

Does anyone have an explanation for this?

Code

#include <iostream>

int main()
{
    const int M = 10;

    int* MPtr = const_cast<int*>(&M);

    (*MPtr)++;

    std::cout << "MPtr = " << MPtr << "   (*MPtr) = " << (*MPtr) << std::endl;
    std::cout << "  &M = " << &M << "         M = " << M << std::endl;
}

Output

MPtr = 0x7fff9b4b6ce0   (*MPtr) = 11
  &M = 0x7fff9b4b6ce0         M = 10

解决方案

So, aside from the "it's undefined behaviour" (which it is), the compiler is perfectly fine to use the fact that M is a constant, thus won't change, in the evaluation of cout ... << M << ..., so can use an instruction that has the immediate value 10, instead of the actual value stored in the memory of M. (Of course, the standard will not say how this works, more than "it's undefined", and compilers are able to choose different solutions in different circumstances, etc, etc, so it's entirely possible that you'll get different results if you modify the code, use a different compiler, different version of compiler or the wind is blowing in a different direction).

Part of the tricky bit with "undefined behaviour" is that it includes things that are "perfectly what you may expect" as well as "nearly what you'd expect". The compiler could also decide to start tetris if it discovers this is what you are doing.

And yes, this is very much one of the reasons why you SHOULD NOT use const_cast. At the very least NOT on things that were originally const - it's OK if you have something along these lines:

int x;

void func(const int* p)
{
  ...
  int *q = const_cast<int *>(p);

  *q = 7;
}


...

 func(&x);

In this case, x is not actually const, it just becomes const when we pass it to func. Of course, the compiler may still assume that x is not changed in func, and thus you could have problems....

这篇关于使用const_cast的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆