Function specifiers
Used in the declaration of functions.
This section is incomplete Reason: split into two pages |
-
-
inline
(since C99) - suggestion to the compiler to "inline" the function, making calls to it as fast as possible. -
_Noreturn
(since C11) - specifies that the function does not return to where it was called from.
-
Contents |
[edit] Syntax
inline function_declaration | |||||||||
_Noreturn function_declaration | |||||||||
[edit] Explanation
[edit] inline (since C99)
The inline
keyword is a hint given to the compiler to perform an optimization. The compiler has the freedom to ignore this request.
If the compiler inlines the function, it replaces every call of that function with the actual body (without generating a call). This avoids extra overhead created by the function call (placing data on stack and retrieving the result) but it may result in a larger executable as the code for the function has to be repeated multiple times. The result is similar to function-like macros.
The function body must be visible in the current translation unit, which makes the inline
keyword necessary to implement functions inside a header file, i.e. without having a source file that has to be compiled and linked.
[edit] inline Example
With inlining turned off, run time is about 1.70 seconds. With inlining turned on, run time is less than a second.
/* inline int sum(int a, int b) { return (a + b); } int c = sum(1, 4); // If the compiler inlines the function the compiled code will be the same as writing: int c = 1 + 4; */ #include <stdio.h> #include <stdlib.h> #include <time.h> /* int sum (int a, int b) __attribute__ ((noinline)); */ static inline int sum (int a, int b) { return a+b; } int main(void) { const int SIZE = 100000; int X[SIZE],Y[SIZE],A[SIZE]; int i,j; for (i=0;i<SIZE;++i) { X[i] = rand(); Y[i] = rand(); } clock_t t = clock(); /* start clock */ for (i=0;i<5000;++i) { for (j=0;j<SIZE;++j) A[j] = sum(X[j],Y[j]); } t = clock()-t; /* stop clock */ printf("Time used: %f seconds\n", ((float)t)/CLOCKS_PER_SEC); printf("%d\n", A[0]); return EXIT_SUCCESS; }
Possible output:
Time used: 0.750000 seconds -1643747027
[edit] _Noreturn (since C11)
The _Noreturn
keyword states that the function that follows it does not return to the function that it was called from. The compiler will typically generate a warning if a function declared with _Noreturn
attempts to return a value.
In the example below the stop_now() function is called, which causes the program to immediately terminate (unless the SIGABRT
signal is caught). The code with the printf() and the return EXIT_SUCCESS; after the call to stop_now() is never executed and the execution never returns to the main() function, where stop_now() was called from.
[edit] _Noreturn Example
#include <stdlib.h> #include <stdio.h> _Noreturn void stop_now() { abort(); } int main(void) { printf("Preparing to stop...\n"); stop_now(); printf("This code is never executed.\n"); return EXIT_SUCCESS; }
Output:
Preparing to stop... Abort