- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我读了this关于 noreturn
属性的问题,该属性用于不返回给调用者的函数。
然后我用C写了一个程序。
#include <stdio.h>
#include <stdnoreturn.h>
noreturn void func()
{
printf("noreturn func\n");
}
int main()
{
func();
}
并使用 this 生成代码汇编:
.LC0:
.string "func"
func:
pushq %rbp
movq %rsp, %rbp
movl $.LC0, %edi
call puts
nop
popq %rbp
ret // ==> Here function return value.
main:
pushq %rbp
movq %rsp, %rbp
movl $0, %eax
call func
为什么函数 func()
在提供 noreturn
属性后返回?
最佳答案
C 中的函数说明符是对编译器的提示,接受程度由实现定义。
首先,_Noreturn
函数说明符(或 noreturn
,使用 <stdnoreturn.h>
)是对编译器的提示,理论上 promise 程序员做出此函数永远不会返回。基于这个 promise ,编译器可以做出某些决定,对代码生成进行一些优化。
IIRC,如果用 noreturn
指定的函数函数说明符最终返回给它的调用者,或者
return
声明behaviour is undefined .您不得从该函数返回。
为了清楚起见,使用 noreturn
函数说明符不停止函数形式返回给它的调用者。这是程序员向编译器做出的 promise ,允许它有更大的自由度来生成优化的代码。
现在,万一你早晚做出了 promise ,选择违背这个,结果就是UB。鼓励但不要求编译器在出现 _Noreturn
时发出警告。函数似乎能够返回其调用者。
根据章节 §6.7.4,C11
, 第 8 段
A function declared with a
_Noreturn
function specifier shall not return to its caller.
还有第 12 段,(注意评论!!)
EXAMPLE 2
_Noreturn void f () {
abort(); // ok
}
_Noreturn void g (int i) { // causes undefined behavior if i <= 0
if (i > 0) abort();
}
对于 C++
,行为非常相似。引自第 §7.6.4 章,C++14
, 第 2 段(强调我的)
If a function
f
is called wheref
was previously declared with thenoreturn
attribute andf
eventually returns, the behavior is undefined. [ Note: The function may terminate by throwing an exception. —end note ][ Note: Implementations are encouraged to issue a warning if a function marked
[[noreturn]]
might return. —end note ]3 [ Example:
[[ noreturn ]] void f() {
throw "error"; // OK
}
[[ noreturn ]] void q(int i) { // behavior is undefined if called with an argument <= 0
if (i > 0)
throw "positive";
}—end example ]
关于c - 为什么 "noreturn"函数返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45981545/
假设我正在制作一个持久的应用程序,并且预计不会因任何正常原因而终止(例如用户终止应用程序,例如:HTTP 服务器)。 我标main本身具有 C++11 标准属性 [[noreturn]] ,表明它绝不
当我使用 clang(版本 3.4(trunk 194574))编译时: typedef void (* FunctionThatNeverReturns [[ noreturn ]])(); 我明白
[dcl.attr.noreturn]可用于标记函数不返回。 [[ noreturn ]] void f() { throw "error"; } [[noreturn]] 是函数标识/签名的
我遇到了这个“高可读性”和“优雅”代码的“美丽”示例,但我在理解它时遇到了麻烦: struct S { [[noreturn]] virtual inline auto f(const uns
当我编译下面的 C 程序时,我得到这个警告:“noreturn”函数确实返回。这是函数: void hello(void){ int i; i=1; } 为什么会这样?对该函数的所有调用都是
[dcl.attr.noreturn]提供以下示例: [[ noreturn ]] void f() { throw "error"; // OK } 但是我不明白[[noreturn
考虑到下面的代码,我或者更确切地说是原始代码的开发者,期望函数本地对象在调用 [[noreturn]] fubar() 之前被销毁。功能。 #include #include using std:
在下面的代码中,两个错误处理程序都被声明为[[noreturn]],我知道如果必须返回值的函数调用一个抛出异常且从不返回的函数,那么这是明确定义的行为。 p> 如果控制到达函数 f 的末尾而没有 re
我在尝试清除 g++ 编译器警告时遇到了一些麻烦。 说我有这门课: class A { public: [[noreturn]] virtual void will_throw() { thr
在处理线程(纤程)调度类时,我发现自己编写了一个永不返回的函数: // New thread, called on an empty stack // (implementation details,
假设我有一个签名为 [[noreturn]] void die(int exit_code); 的函数。如果我写声明: check_some_condition() or die(EXIT_FAILU
void sbs(){ exit(0); } 警告: function might be possible candidate for attribute ‘noreturn’ 得
这个问题在这里已经有了答案: Is there a standard "never returns" attribute for C# functions? (10 个答案) 关闭 8 年前。 C#
有时我想写一个 error() 函数,它最终肯定会调用 System.exit() ,这意味着这个函数永远不会返回。但是,如果我在其他函数中调用 error(),我想这样写: int fun() {
永不返回函数的 noreturn 属性是否必要,或者这只是一个(可以说是过早的?——至少对于退出,我无法想象为什么要在那里优化)优化? 有人向我解释说,在诸如 void myexit(int s) _
我正在用 C11 编写引导加载程序。当引导加载程序需要将控制权转移给固件时,它会在预定义的内存地址读取一个函数指针并调用它。代码如下所示: typedef void (FirmwareBootFn)(
我读了this关于 noreturn 属性的问题,该属性用于不返回给调用者的函数。 然后我用C写了一个程序。 #include #include noreturn void func() {
[[noreturn]] 属性可以应用于不打算返回的函数。例如: [[noreturn]] void will_throw() { throw std::runtime_error("bad, bad
来自另一个 question 中关于使用供应商特定属性的讨论我问自己, “对于使用标准中未列出的属性,我们应该告诉人们什么规则” ? 定义的两个属性是 [[ noreturn ]]和 [[ carri
我在编译 native C++ dll(在 VS2008 SP1 上)时出现编译错误。 错误是: error C2062: type 'void' unexpected c:\Progr
我是一名优秀的程序员,十分优秀!