我试图使一个Python程序接口与不同的崩溃进程(这是我的手)。不幸的是,我正在使用的程序甚至不会可靠地崩溃!所以我想做一个快速的c++程序,它会故意崩溃,但我不知道最好和最短的方法来做到这一点,有人知道在我的

int main() {
    crashyCodeGoesHere();
}

使我的c++程序可靠地崩溃


当前回答

简单的缓冲区溢出代码,将导致程序崩溃

int main()
{
    int n[0];
    n[2] = 0;
}

其他回答

char*freeThis;
free(freeThis);

释放未初始化的指针是未定义的行为。在许多平台/编译器上,freeThis将有一个随机值(无论之前在内存位置上是什么)。释放它将要求系统释放该地址的内存,这通常会导致分割错误并使程序崩溃。

 throw 42;

只要回答……:)

int i = 1 / 0;

您的编译器可能会警告您这一点,但在GCC 4.4.3下它编译得很好 这可能会导致SIGFPE(浮点异常),这在实际应用程序中可能不像SIGSEGV(内存分段违反)那样可能导致其他答案,但它仍然是崩溃。在我看来,这样可读性更强。

另一种方法,如果我们要欺骗和使用signal.h,是:

#include <signal.h>
int main() {
    raise(SIGKILL);
}

这保证杀死子进程,与SIGSEGV形成对比。

我看到这里张贴了许多答案,它们将成为完成工作的幸运案例,但没有一个是100%确定崩溃的。一些会在一个硬件和操作系统上崩溃,另一些不会。 然而,根据官方c++标准,有一种标准方法可以使它崩溃。

引用c++标准ISO/IEC 14882§15.1-7:

If the exception handling mechanism, after completing the initialization of the exception object but before the activation of a handler for the exception, calls a function that exits via an exception, std::terminate is called (15.5.1). struct C { C() { } C(const C&) { if (std::uncaught_exceptions()) { throw 0; // throw during copy to handler’s exception-declaration object (15.3) } } }; int main() { try { throw C(); // calls std::terminate() if construction of the handler’s // exception-declaration object is not elided (12.8) } catch(C) { } }

我写了一个小代码来演示这一点,可以在这里找到并在Ideone上试用。

class MyClass{
    public:
    ~MyClass() throw(int) { throw 0;}
};

int main() {
  try {
    MyClass myobj; // its destructor will cause an exception

    // This is another exception along with exception due to destructor of myobj and will cause app to terminate
     throw 1;      // It could be some function call which can result in exception.
  }
  catch(...)
  {
    std::cout<<"Exception catched"<<endl;
  }
  return 0;
}

ISO/IEC 14882§15.1/9提到throw没有try块导致隐式调用abort:

如果当前没有处理异常,则执行 没有操作数的抛出表达式调用std::terminate()

其他包括: throw from析构函数:ISO/IEC 14882§15.2/3

*( ( char* ) NULL ) = 0;

这将产生一个分割错误。