__builtin_trap:何时使用? [英] __builtin_trap: when to use it?

查看:752
本文介绍了__builtin_trap:何时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gcc提供了用于优化的其他内置函数。

gcc provides additional builtin functions "for optimization".

其中之一是 void __builtin_trap(void)

从文档中:


__ builtin_trap函数导致程序异常退出。 GCC通过使用依赖于目标的机制(例如,故意执行非法指令的
)或调用abort来实现此
函数。
使用的机制可能因发行版本而异,因此您不应依赖任何
特定的实现。

__builtin_trap function causes the program to exit abnormally. GCC implements this function by using a target-dependent mechanism (such as intentionally executing an illegal instruction) or by calling abort. The mechanism used may vary from release to release so you should not rely on any particular implementation.

为什么您会使用此方法而不是 exit(1) abort ?为什么gcc开发人员将其视为优化功能?

Why would you ever use this rather than exit(1) or abort? Why did the gcc developers see this as an optimization function?

推荐答案

因为 exit(1)使程序正常终止并显示错误状态码。请参见 cppreference页面。相反, __ builtin_trap 导致程序异常终止。

Because exit(1) causes the program to terminate normally with an error status code. See the cppreference page. In contrast __builtin_trap causes the program to terminate abnormally.

查看差异的最简单方法是查看 exit 做出的保证,如果您不想做其中一件事情,则 __ 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.

调试是最常见的示例,因为 __ builtin_trap 可能会触发调试器转储进程,而 exit 不会(因为程序正常以错误终止)。

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:何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆