Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of _Noreturn in C11 [duplicate]

Tags:

Possible Duplicate:
What is the point of the Noreturn attribute?

C11 introduced the _Noreturn attribute to indicate that a function never returns.

Except for documentation value in the source code, what other benefits do the attribute provide, and why would one use it ?

like image 328
user1255770 Avatar asked Dec 03 '12 20:12

user1255770


People also ask

What does [[ Noreturn ]] do?

__attribute__((noreturn)) function attribute This function attribute informs the compiler that the function does not return. The compiler can then perform optimizations by removing the code that is never reached.

What is Noreturn?

C++ attribute: noreturn (since C++11)Indicates that the function does not return.


1 Answers

If a function calls unconditionally a _Noreturn function, the compiler will be able to understand that:

  • the code that follows is dead code, which allows for optimization (it can be removed from the generated binary) and diagnostics - the compiler will be able to emit a "non-reachable code" warnings;

  • most importantly, knowing that the normal flow from the function is interrupted it will be able to avoid spurious warnings about missing return values, uninitialized variables and the like.

    This is particularly important with static code analyzers - the number of false positives given by CLang static analyzer in a biggish application of ours dropped considerably once we marked our die function (log a fatal error and terminate the application) as noreturn.

There may be also some other optimization available - since the function never returns, there's no need to push the return address on the stack, save the state of the registers and whatever, all that's needed is to pass the arguments and do a jmp to the start of the function, without worrying about the return and the post-return cleanup. But of course, since the call is one-shot, the performance to squeeze here is mostly negligible.

like image 114
Matteo Italia Avatar answered Oct 20 '22 07:10

Matteo Italia