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

int main() {
    crashyCodeGoesHere();
}

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


当前回答

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

void main(){
    throw 1;
}

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

其他回答

写入只读内存会导致分割错误,除非你的系统不支持只读内存块。

int main() {
    (int&)main = 0;
}

我在Windows 7上用MingGW 5.3.0测试了它,在Linux Mint上用GCC测试了它。我认为其他编译器和系统也会有类似的效果。

#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;
}

这个缺失了:

int main = 42;
int* p=0;
*p=0;

这也应该崩溃。在Windows上,它与AccessViolation一起崩溃,我猜它应该在所有操作系统上都是一样的。

 throw 42;

只要回答……:)