使 C++ 程序崩溃的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8481783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the easiest way to make a C++ program crash?
提问by jonathan topf
I'm trying to make a Python program that interfaces with a different crashy process (that's out of my hands). Unfortunately the program I'm interfacing with doesn't even crash reliably! So I want to make a quick C++ program that crashes on purpose but I don't actually know the best and shortest way to do that, does anyone know what to put between my:
我正在尝试制作一个 Python 程序,该程序与不同的崩溃进程(这是我无法控制的)交互。不幸的是,我与之交互的程序甚至不能可靠地崩溃!所以我想制作一个故意崩溃的快速 C++ 程序,但我实际上不知道最好和最短的方法来做到这一点,有没有人知道在我的:
int main() {
crashyCodeGoesHere();
}
to make my C++ program crash reliably
使我的 C++ 程序可靠地崩溃
采纳答案by duskwuff -inactive-
The abort()
function is probably your best bet. It's part of the C standard library, and is defined as "causing abnormal program termination" (e.g, a fatal error or crash).
该abort()
功能可能是您最好的选择。它是 C 标准库的一部分,被定义为“导致程序异常终止”(例如,致命错误或崩溃)。
回答by Martin York
Try:
尝试:
raise(SIGSEGV); // simulates a standard crash when access invalid memory
// ie anything that can go wrong with pointers.
Found in:
在发现:
#include <signal.h>
回答by Roee Gavirel
Dividing by zero will crash the application:
除以零将使应用程序崩溃:
int main()
{
return 1 / 0;
}
回答by Keith Nicholas
*((unsigned int*)0) = 0xDEAD;
回答by sam hocevar
Well, are we on stackoverflow, or not?
好吧,我们是否处于堆栈溢出状态?
for (long long int i = 0; ++i; (&i)[i] = i);
(Not guaranteed to crash by any standards, but neither are any of the suggested answers including the accepted one since SIGABRT
could have been caught anyway. In practice, this will crash everywhere.)
(不保证任何标准都会崩溃,但任何建议的答案(包括已接受的答案)都没有,因为SIGABRT
无论如何都可能被捕获。实际上,这将在任何地方崩溃。)
回答by Macke
throw 42;
Just the answer... :)
只是答案... :)
回答by Dan F
assert(false);
is pretty good too.
assert(false);
也不错。
According to ISO/IEC 9899:1999 it is guaranteed to crash when NDEBUG is not defined:
根据 ISO/IEC 9899:1999,当未定义 NDEBUG 时,保证会崩溃:
If NDEBUG is defined [...] the assert macro is defined simply as
#define assert(ignore) ((void)0)
The assert macro is redefined according to the current state of NDEBUG each time that is included.
[...]
The assert macro puts diagnostic tests into programs; [...] if expression (which shall have a scalar type) is false [...]. It then calls the abort function.
如果定义了 NDEBUG [...],则断言宏定义为
#define assert(ignore) ((void)0)
每次包含时,都会根据 NDEBUG 的当前状态重新定义 assert 宏。
[...]
assert 宏将诊断测试放入程序中;[...] 如果表达式(应具有标量类型)为 false [...]。然后调用 abort 函数。
回答by PlasmaHH
Since a crash is a symptom of invoking undefined behaviour, and since invoking undefined behaviour can lead to anything, including a crash, I don't think you want to really crash your program, but just have it drop into a debugger. The most portable way to do so is probably abort()
.
由于崩溃是调用未定义行为的症状,并且调用未定义行为可能导致任何事情,包括崩溃,我认为您不想真正使程序崩溃,而只是将其放入调试器中。最便携的方法可能是abort()
.
While raise(SIGABRT)
has the same effect, it is certainly more to write. Both ways however can be intercepted by installing a signal handler for SIGABRT
. So depending on your situation, you might want/need to raise another signal. SIGFPE
, SIGILL
, SIGINT
, SIGTERM
or SIGSEGV
might be the way to go, but they all can be intercepted.
虽然raise(SIGABRT)
有同样的效果,但肯定是多写。然而,这两种方式都可以通过为 安装信号处理程序来拦截SIGABRT
。因此,根据您的情况,您可能想要/需要发出另一个信号。SIGFPE
, SIGILL
, SIGINT
,SIGTERM
或SIGSEGV
可能是要走的路,但它们都可以被拦截。
When you can be unportable, your choices might be even broader, like using SIGBUS
on linux.
当您无法移植时,您的选择可能会更广泛,例如SIGBUS
在 linux 上使用。
回答by Paul Biggar
The answer is platform specific and depends on your goals. But here's the Mozilla Javascript crash function, which I think illustrates a lot of the challenges to making this work:
答案是特定于平台的,取决于您的目标。但这里是 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
}
回答by samridhi
The only flash I had is abort() function:
我唯一的闪光是abort() 函数:
It aborts the process with an abnormal program termination.It generates the SIGABRT signal, which by default causes the program to terminate returning an unsuccessful termination error code to the host environment.The program is terminated without executing destructorsfor objects of automatic or static storage duration, and without callingany atexit( which is called by exit() before the program terminates)function. It never returns to its caller.
它在程序异常终止时中止进程。它生成SIGABRT 信号,默认情况下会导致程序终止,向宿主环境返回一个不成功的终止错误代码。程序终止而不执行自动或静态存储持续时间的对象的析构函数,并且不调用任何atexit(在程序终止之前由 exit() 调用)函数。它永远不会返回给它的调用者。