本文介绍了为什么“不归还"函数返回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了关于noreturn属性的问题对于不返回调用方的功能.

I read this question about noreturn attribute, which is used for functions that don't return to the caller.

然后我用C语言编写了一个程序.

Then I have made a program in C.

#include <stdio.h>
#include <stdnoreturn.h>

noreturn void func()
{
        printf("noreturn func\n");
}

int main()
{
        func();
}

并使用生成的代码汇编:

And generated assembly of the code using 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

为什么提供noreturn属性后函数func()返回?

Why does function func() return after providing noreturn attribute?

推荐答案

C中的函数说明符是编译器的提示,接受程度由实现定义.

The function specifiers in C are a hint to the compiler, the degree of acceptance is implementation defined.

首先,_Noreturn函数说明符(或noreturn,使用<stdnoreturn.h>)向编译器提示程序员关于理论承诺的信息,即该函数永远不会返回.基于此承诺,编译器可以做出某些决定,并对代码生成执行一些优化.

First of all, _Noreturn function specifier (or, noreturn, using <stdnoreturn.h>) is a hint to the compiler about a theoretical promise made by the programmer that this function will never return. Based on this promise, compiler can make certain decisions, perform some optimizations for the code generation.

IIRC,如果使用noreturn函数说明符指定的函数最终返回到其调用者,则

IIRC, if a function specified with noreturn function specifier eventually returns to its caller, either

  • 使用明确的return语句
  • 到达功能主体的末端

行为未定义.您绝对不能从函数返回.

为了清楚起见,使用noreturn函数说明符不会停止函数窗体返回其调用者.程序员对编译器的承诺是允许编译器具有更大的自由度来生成优化的代码.

To make it clear, using noreturn function specifier does not stop a function form returning to its caller. It is a promise made by the programmer to the compiler to allow it some more degree of freedom to generate optimized code.

现在,以防万一,您早晚做出诺言,选择违反此诺言,结果就是UB.当_Noreturn函数似乎能够返回其调用者时,鼓励(但不是必须)编译器发出警告.

Now, in case, you made a promise earlier and later, choose to violate this, the result is UB. Compilers are encouraged, but not required, to produce warnings when a _Noreturn function appears to be capable of returning to its caller.

根据§6.7.4第C11节第8段

According to chapter §6.7.4, C11, Paragraph 8

以及第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段(强调我的)


For C++, the behaviour is quite similar. Quoting from chapter §7.6.4, C++14, paragraph 2 (emphasis mine)

[注意:如果标有[[noreturn]]的功能可能会鼓励实现发出警告 返回. —尾注]

[ Note: Implementations are encouraged to issue a warning if a function marked [[noreturn]] might return. —end note ]

3 [示例:

[[ 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";
}

-结束示例]

这篇关于为什么“不归还"函数返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 15:05