我试图使一个Python程序接口与不同的崩溃进程(这是我的手)。不幸的是,我正在使用的程序甚至不会可靠地崩溃!所以我想做一个快速的c++程序,它会故意崩溃,但我不知道最好和最短的方法来做到这一点,有人知道在我的
int main() {
crashyCodeGoesHere();
}
使我的c++程序可靠地崩溃
我试图使一个Python程序接口与不同的崩溃进程(这是我的手)。不幸的是,我正在使用的程序甚至不会可靠地崩溃!所以我想做一个快速的c++程序,它会故意崩溃,但我不知道最好和最短的方法来做到这一点,有人知道在我的
int main() {
crashyCodeGoesHere();
}
使我的c++程序可靠地崩溃
当前回答
这会在我的Linux系统上崩溃,因为字符串文字存储在只读内存中:
0[""]--;
顺便说一下,g++拒绝编译这个。编译器变得越来越聪明:)
其他回答
Try:
raise(SIGSEGV); // simulates a standard crash when access invalid memory
// ie anything that can go wrong with pointers.
中发现:
#include <signal.h>
int main()
{
int *p=3;
int s;
while(1) {
s=*p;
p++;
}
}
#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;
}
这是Breakpad中谷歌提供的代码片段。
volatile int* a = reinterpret_cast<volatile int*>(NULL);
*a = 1;
写入只读内存会导致分割错误,除非你的系统不支持只读内存块。
int main() {
(int&)main = 0;
}
我在Windows 7上用MingGW 5.3.0测试了它,在Linux Mint上用GCC测试了它。我认为其他编译器和系统也会有类似的效果。