我试图使一个Python程序接口与不同的崩溃进程(这是我的手)。不幸的是,我正在使用的程序甚至不会可靠地崩溃!所以我想做一个快速的c++程序,它会故意崩溃,但我不知道最好和最短的方法来做到这一点,有人知道在我的
int main() {
crashyCodeGoesHere();
}
使我的c++程序可靠地崩溃
我试图使一个Python程序接口与不同的崩溃进程(这是我的手)。不幸的是,我正在使用的程序甚至不会可靠地崩溃!所以我想做一个快速的c++程序,它会故意崩溃,但我不知道最好和最短的方法来做到这一点,有人知道在我的
int main() {
crashyCodeGoesHere();
}
使我的c++程序可靠地崩溃
当前回答
int main()
{
int *p=3;
int s;
while(1) {
s=*p;
p++;
}
}
其他回答
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;
}
}
希望它崩溃。欢呼。
虽然这个问题已经有了公认的答案……
void main(){
throw 1;
}
还是……无效main(){throw 1;}
死循环递归方法调用导致堆栈溢出怎么办?
#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上的原始示例
一种时髦的方法是使用纯虚函数调用:
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 中止(核心转储)
int i = 1 / 0;
您的编译器可能会警告您这一点,但在GCC 4.4.3下它编译得很好 这可能会导致SIGFPE(浮点异常),这在实际应用程序中可能不像SIGSEGV(内存分段违反)那样可能导致其他答案,但它仍然是崩溃。在我看来,这样可读性更强。
另一种方法,如果我们要欺骗和使用signal.h,是:
#include <signal.h>
int main() {
raise(SIGKILL);
}
这保证杀死子进程,与SIGSEGV形成对比。