我试图使一个Python程序接口与不同的崩溃进程(这是我的手)。不幸的是,我正在使用的程序甚至不会可靠地崩溃!所以我想做一个快速的c++程序,它会故意崩溃,但我不知道最好和最短的方法来做到这一点,有人知道在我的
int main() {
crashyCodeGoesHere();
}
使我的c++程序可靠地崩溃
我试图使一个Python程序接口与不同的崩溃进程(这是我的手)。不幸的是,我正在使用的程序甚至不会可靠地崩溃!所以我想做一个快速的c++程序,它会故意崩溃,但我不知道最好和最短的方法来做到这一点,有人知道在我的
int main() {
crashyCodeGoesHere();
}
使我的c++程序可靠地崩溃
当前回答
答案是平台特定的,取决于你的目标。但这里是Mozilla Javascript崩溃函数,我认为这说明了很多挑战,使这个工作:
static JS_NEVER_INLINE void
CrashInJS()
{
/*
* We write 123 here so that the machine code for this function is
* unique. Otherwise the linker, trying to be smart, might use the
* same code for CrashInJS and for some other function. That
* messes up the signature in minidumps.
*/
#if defined(WIN32)
/*
* We used to call DebugBreak() on Windows, but amazingly, it causes
* the MSVS 2010 debugger not to be able to recover a call stack.
*/
*((int *) NULL) = 123;
exit(3);
#elif defined(__APPLE__)
/*
* On Mac OS X, Breakpad ignores signals. Only real Mach exceptions are
* trapped.
*/
*((int *) NULL) = 123; /* To continue from here in GDB: "return" then "continue". */
raise(SIGABRT); /* In case above statement gets nixed by the optimizer. */
#else
raise(SIGABRT); /* To continue from here in GDB: "signal 0". */
#endif
}
其他回答
除以0会导致应用程序崩溃:
int main()
{
return 1 / 0;
}
这会在我的Linux系统上崩溃,因为字符串文字存储在只读内存中:
0[""]--;
顺便说一下,g++拒绝编译这个。编译器变得越来越聪明:)
*((unsigned int*)0) = 0xDEAD;
在GCC或clang中使用__builtin_trap(),在MSVC中使用__debugbreak()。不处理这些断点/陷阱将导致未处理的断点异常/崩溃。 使用abort()或exit()的其他建议:这些可能由其他线程处理,使得更难看到传播崩溃的线程的堆栈。
或者另一种方式,因为我们都是乐队的。
一个可爱的无限递归。保证会让你大吃一惊。
int main(int argv, char* argc)
{
return main(argv, argc)
}
打印出:
分段错误(核心转储)