When is the inline keyword effectively used in C?

Well, the standard does not guarantee that inline functions are actually built-in; you need to use macros to have a 100% guarantee. The compiler always decides which function is or is not built-in based on its own rules, regardless of the inline .

Then when does the inline really influence what the compiler does when using modern compilers like the recent version of GCC?

+8
c inline-functions
source share
2 answers

It has a semantic effect. For simplicity, the function marked inline can be defined several times in one program - although all definitions must be equivalent to each other. therefore, for correctness, the presence of inline is required when the function definition is included in the headers (which, in turn, makes the definition visible so that the compiler can embed it without LTO).

In addition, for inline optimization, "never" is an absolutely safe approximation. This probably has some effect on some compilers, but nothing is worth losing sleep, especially not without real hard data. For example, in the following code using Clang 3.0 or GCC 4.7 , main contains the same code whether work inline checked or not. The only difference is whether work remains an autonomous function for other translation units to reference or be deleted.

 void work(double *a, double *b) { if (*b > *a) *a = *b; } void maxArray(double* x, double* y) { for (int i = 0; i < 65536; i++) { //if (y[i] > x[i]) x[i] = y[i]; work(x+i, y+i); } } 
+4
source share

If you want to control inlining, stick to any pragmas or attributes your compiler provides to control this behavior. For example __attribute__((always_inline)) for GCC and similar compilers. As you already mentioned, the inline often ignored depending on optimization parameters, etc.

+1
source share

All Articles