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

int main() {
    crashyCodeGoesHere();
}

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


当前回答

一种时髦的方法是使用纯虚函数调用:

class Base;

void func(Base*);

class Base
{
public:
   virtual void f() = 0;
   Base() 
   {
       func(this);
   }
};

class Derived : Base
{
   virtual void f()
   {
   }
};

void func(Base* p)
{
   p->f();
}


int main()
{
    Derived  d;
}

用gcc编译,输出:

纯虚方法调用 在没有活动异常时调用Terminate 中止(核心转储)

其他回答

#include <thread>

void intentionalCrash()
{
    auto noop = [](){return;};
    // Thread t1 is in a joinable state.
    // When program returns t1 will be out of scope.
    // Destructing a joinable thread creates a crash.
    std::thread t1(noop);
}

int main()
{
    intentionalCrash();
    return 0;
}

我们到底是不是在stackoverflow上?

for (long long int i = 0; ++i; (&i)[i] = i);

(根据任何标准都不保证会崩溃,但也没有任何建议的答案,包括被接受的答案,因为SIGABRT无论如何都可能被捕获。实际上,这会在任何地方崩溃。)

*( ( char* ) NULL ) = 0;

这将产生一个分割错误。

虽然这个问题已经有了公认的答案……

void main(){
    throw 1;
}

还是……无效main(){throw 1;}

 throw 42;

只要回答……:)