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

int main() {
    crashyCodeGoesHere();
}

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


当前回答

或者另一种方式,因为我们都是乐队的。

一个可爱的无限递归。保证会让你大吃一惊。

int main(int argv, char* argc)
{
   return main(argv, argc)
}

打印出:

分段错误(核心转储)

其他回答

 throw 42;

只要回答……:)

死循环递归方法调用导致堆栈溢出怎么办?

#include <windows.h>
#include <stdio.h>

void main()
{
    StackOverflow(0);
}

void StackOverflow(int depth)
{
    char blockdata[10000];
    printf("Overflow: %d\n", depth);
    StackOverflow(depth+1);
}

请参阅Microsoft KB上的原始示例

Try:

raise(SIGSEGV);  // simulates a standard crash when access invalid memory
                 // ie anything that can go wrong with pointers.

中发现:

#include <signal.h>
*( ( char* ) NULL ) = 0;

这将产生一个分割错误。

这会在我的Linux系统上崩溃,因为字符串文字存储在只读内存中:

0[""]--;

顺便说一下,g++拒绝编译这个。编译器变得越来越聪明:)