相关疑难解决方法(0)

由于未定义的行为或编译器错误导致C++代码崩溃?

我遇到了奇怪的崩溃.我想知道它是否是我的代码或编译器中的错误.当我使用Microsoft Visual Studio 2010将以下C++代码编译为优化的发布版本时,它会在标记的行中崩溃:

struct tup { int x; int y; };

class C 
{
public:
  struct tup* p;

  struct tup* operator--() { return --p; }
  struct tup* operator++(int) { return p++; }

  virtual void Reset() { p = 0;}
};

int main ()
{
  C c;
  volatile int x = 0;
  struct tup v1;
  struct tup v2 = {0, x};

  c.p = &v1;
  (*(c++)) = v2;

  struct tup i = (*(--c));   // crash! (dereferencing a NULL-pointer)
  return i.x;
} …
Run Code Online (Sandbox Code Playgroud)

c++ crash visual-studio-2010 compiler-optimization visual-c++

16
推荐指数
1
解决办法
714
查看次数