[英]__builtin_trap: when to use it?
gcc provides additional builtin functions "for optimization". gcc提供了“用于优化”的其他内置函数。
One of them is void __builtin_trap (void)
which essentially is here to abort the program by executing an illegal command. 其中一个是
void __builtin_trap (void)
,它实际上是通过执行非法命令来中止程序。
From the doc: 来自doc:
__builtin_trap function causes the program to exit abnormally.
__builtin_trap函数导致程序异常退出。 GCC implements this function by using a target-dependent mechanism (such as intentionally executing an illegal instruction) or by calling abort.
GCC通过使用依赖于目标的机制(例如故意执行非法指令)或通过调用abort来实现此功能。 The mechanism used may vary from release to release so you should not rely on any particular implementation.
使用的机制可能因版本而异,因此您不应依赖任何特定实现。
Why would you ever use this rather than exit(1)
or abort
? 你为什么要使用它而不是
exit(1)
或abort
? Why did the gcc developers see this as an optimization function? 为什么gcc开发人员将此视为优化功能?
Because exit(1)
causes the program to terminate normally with an error status code. 因为
exit(1)
导致程序正常终止并带有错误状态代码。 See the cppreference page . 请参阅cppreference页面 。 In contrast
__builtin_trap
causes the program to terminate abnormally. 相反,
__builtin_trap
导致程序异常终止。
The simplest way to see the differences is to look at the guarantees made by exit
, if doing one of those things is something you don't want to happen, __builtin_trap
would be better. 查看差异的最简单方法是查看
exit
所做的保证,如果其中一件事情是你不想发生的事情,那么__builtin_trap
会更好。
Debugging is the most common example, as __builtin_trap
could trigger a debugger to dump the process, while exit
would not (since the program terminates "normally" with an error). 调试是最常见的示例,因为
__builtin_trap
可以触发调试器来转储进程,而exit
不会(因为程序“正常”终止并出现错误)。
The __builtin
functions are not necessarily for optimisation - they are for "doing things that the compiler can't do directly from source code", including support for "special instructions" and "architecture-specific operations". __builtin
函数不一定用于优化 - 它们用于“执行编译器不能直接从源代码执行的操作”,包括对“特殊指令”和“特定于体系结构的操作”的支持。 One of the main purposes of __builtin
functions is that the compiler will "know" what these do at a later stage. __builtin
函数的主要目的之一是编译器将“知道”它们在稍后阶段的作用。 Although there are "library optimisations" in the compiler, the compiler can use __builtin
functions much more freely to determine that the behaviour is something specific - for example, __builtin_trap
can be relied upon to "not continue to the next instruction", so the compiler doesn't have to worry about code like: 虽然编译器中存在“库优化”,但编译器可以更自由地使用
__builtin
函数来确定行为是特定的 - 例如, __builtin_trap
可以依赖于“不继续下一条指令”,因此编译器不必担心像这样的代码:
if (x <= 0.0) __builtin_trap();
y = ln(x);
It can then make use of the "fast inline version of ln
", since the error has been caught already. 然后它可以使用“快速内联版本的
ln
”,因为错误已经被捕获。
Note also that __builtin_trap
is almost guaranteed to end up as a "stop" in the debugger, where exit(1)
or some such will just exit the program with a "not success" result code, which is rather annoying if you are trying to figure out where that math error message came from... 另请注意,
__builtin_trap
几乎可以保证在调试器中作为“停止”结束,其中exit(1)
或其他一些只会以“not success”结果代码退出程序,如果你想尝试找出那个数学错误信息来自哪里......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.