Inline (C and C++)
In the C and C++ programming languages, an inline function is one qualified with the keyword inline ; this serves two purposes: # It serves as a compiler directive that suggests (but does not require) that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function call, thereby saving the overhead of a function call. In this respect it is analogous to the register storage class specifier, which similarly provides an optimization hint. # The second purpose of inline is to change linkage behavior. This is necessary due to the C/C++ separate compilation + linkage model, specifically because the... Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.
In the C and C++ programming languages, an inline function is one qualified with the keyword inline ; this serves two purposes: It serves as a compiler directive that suggests (but does not require) that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function call, thereby saving the overhead of a function call. In this respect it is analogous to the register storage class specifier, which similarly provides an optimization hint. The second purpose of inline is to change linkage behavior. This is necessary due to the C/C++ separate compilation + linkage model, specifically because the definition (body) of the function must be duplicated in all translation units where it is used, to allow inlining during compiling, which, if the function has external linkage, causes a collision during linking (it violates uniqueness of external symbols). C and C++ (and dialects such as GNU C and Visual C++) resolve this in different ways. inline void swap(int* m, int* n) { int tmp = *m; *m = *n; *n = tmp; } Then, a statement such as the following: swap(&x, &y); may be translated into (if the compiler decides to do the inlining, which typically requires optimization to be enabled): int tmp = x; x = y; y = tmp; When implementing a sorting algorithm doing lots of swaps, this can increase the execution speed. ifdef _MSC_VER #define forceinline __forceinline elif defined(__GNUC__) #define forceinline inline __attribute__((__always_inline__)) elif defined(__CLANG__) #if __has_attribute(__always_inline__) #define forceinline inline __attribute__((__always_inline__)) #else #define forceinline inline #endif else #define forceinline inline endif gcc up to and including version 4.2 used gnu89 inline semantics even when -std=c99 was explicitly specified. With version 5, gcc switched from gnu89 to the gnu11 dialect, effectively enabling C99 inline semantics by default. To use gnu89 semantics instead, they have to be enabled explicitly, either with -std=gnu89 or, to only affect inlining, -fgnu89-inline , or by adding the gnu_inline attribute to all inline declarations. To ensure C99 semantics, either -std=c99 , -std=c11 , -std=gnu99 or -std=gnu11 (without -fgnu89-inline ) can be used. ==Problems== Besides the problems with inline expansion in general (see ), inline functions as a language feature may not be as valuable as they appear, for a number of reasons: Often, a compiler is in a better position than a human to decide whether a particular function should be inlined. Sometimes the compiler may not be able to inline as many functions as the programmer indicates. An important point to note is that the code (of the inline function) gets exposed to its client (the calling function). As functions evolve, they may become suitable for inlining where they were not before, or no longer suitable for inlining where they were before. While inlining or un-inlining a function is easier than converting to and from macros, it still requires extra maintenance which typically yields relatively little benefit. Inline functions used in proliferation in native C-based compilation systems can increase compilation time, since the intermediate representation of their bodies is copied into each call site. The specification of inline in C99 requires exactly one external definition of the function, if it is used somewhere. If such a definition wasn't provided by the programmer, that can easily lead to linker errors. This can happen with optimization turned off, which typically prevents inlining. Adding the definitions, on the other hand, can cause unreachable code if the programmer does not carefully avoid it, by putting them in a library for linking, using link time optimization, or static inline . In C++, it is necessary to define an inline function in every module (translation unit) that uses it, whereas an ordinary function must be defined in only a single module. Otherwise it would not be possible to compile a single module independently of all other modules. Depending on the compiler, this may cause each respective object file to contain a copy of the function's code, for each module with some use that could not be inlined. In embedded software, oftentimes certain functions need to be placed in certain code sections by use of special compiler instructions such as "pragma" statements. Sometimes, a function in one memory segment might need to call a function in another memory segment, and if inlining of the called function occurs, then the code of the called function might end up in a segment where it shouldn't be. For example, high-performance memory segments may be very limited in code space, and if a function belonging in such a space calls another large function that is not meant to be in the high-performance section and the called function gets inappropriately inlined, then this might cause the high-performance memory segment to run out of code space. For this reason, sometimes it is necessary to ensure that functions do not become inlined.
Cite this
"Inline (C and C++)". 1782739738. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1361700182 JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy