我试图使一个Python程序接口与不同的崩溃进程(这是我的手)。不幸的是,我正在使用的程序甚至不会可靠地崩溃!所以我想做一个快速的c++程序,它会故意崩溃,但我不知道最好和最短的方法来做到这一点,有人知道在我的
int main() {
crashyCodeGoesHere();
}
使我的c++程序可靠地崩溃
我试图使一个Python程序接口与不同的崩溃进程(这是我的手)。不幸的是,我正在使用的程序甚至不会可靠地崩溃!所以我想做一个快速的c++程序,它会故意崩溃,但我不知道最好和最短的方法来做到这一点,有人知道在我的
int main() {
crashyCodeGoesHere();
}
使我的c++程序可靠地崩溃
当前回答
*( ( char* ) NULL ) = 0;
这将产生一个分割错误。
其他回答
这个缺失了:
int main = 42;
void main()
{
int *aNumber = (int*) malloc(sizeof(int));
int j = 10;
for(int i = 2; i <= j; ++i)
{
aNumber = (int*) realloc(aNumber, sizeof(int) * i);
j += 10;
}
}
希望它崩溃。欢呼。
死循环递归方法调用导致堆栈溢出怎么办?
#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上的原始示例
你可以在c++代码中使用汇编,但是INT 3只适用于x86系统,其他系统可能有其他的陷阱/断点指令。
int main()
{
__asm int 3;
return 0;
}
INT 3导致中断并调用由OS设置的中断向量。
这是上面回答中给出的一个更有保证的abort版本。它负责处理sigabrt被阻塞时的情况。实际上,您可以使用任何信号来代替具有使程序崩溃的默认操作的abort。
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
int main()
{
sigset_t act;
sigemptyset(&act);
sigfillset(&act);
sigprocmask(SIG_UNBLOCK,&act,NULL);
abort();
}