What is an inline function

In C language, if some functions are frequently called and continuously pushed, that is, function stack, it will cause a large consumption of stack space or stack memory.

To solve this problem, the inline modifier, which represents an inline function, has been specifically introduced.

Stack space refers to the memory space for placing the local data of the program, that is, the data in the function. Under the system, stack space is limited. If it is used frequently and in large quantities, the program error caused by insufficient stack space will be caused, and the final result of the recursive call of the function is the exhaustion of stack memory space.

Here’s an example:

#include <stdio.h>  
 
// The function is defined inline, that is, inline
inline const char* dbtest(int i) 
{  
	return (i % 2 > 0)?"Hello" : "偶";  
}   
  
int main(a)  
{  
	int i = 0;  
	for (i=1; i < 100; i++) 
	{  
		printf("I :%d parity :%s /n", i, dbtest(i)); }}Copy the code

The above example uses the standard inline modifier. The inline modifier does not appear to be inline, but is replaced by (I %2>0) wherever dbtest(I) is called inside each for loop. Odd “:” even “this avoids frequent function calls to the stack of memory repeatedly open up the consumption.

This is similar to the dynamic and static library problem we learned earlier. The code in dbtest is placed directly in main, and the for loop calls this code over and over again, rather than creating a stack of functions.

Second, the programming style of inline functions

The keyword inline must be placed with the body of the function definition for the function to be called inline. Simply placing inline before the function declaration does not work.

Foo cannot be an inline function of the following style:

inline void Foo(int x, int y); // Inline is used only with function declarations
void Foo(int x, int y)
{}Copy the code

Foo, however, is an inline function:

void Foo(int x, int y);
inline void Foo(int x, int y) // Inline goes with the function definition body
{}Copy the code

So inline is a “keyword for implementation”, not a “keyword for declaration”. Generally, the user can read the declaration of the function, but not the definition of the function. Although inline is used in most textbooks before the declaration and definition body of an inline function, I don’t think inline should be used in a function declaration. This detail does not affect function functionality, but it illustrates a basic tenet of high-quality C++/C programming style: declaration and definition are not to be confused, and users do not need or should know whether functions need to be inlining.

The use of inline is limited

Inline is only suitable for functions with simple code inside an inclusion. It cannot contain complex structural control statements such as while or switch. Inline functions themselves cannot be direct recursive functions (they call their own functions internally).

Three, careful use of inline

Inlining can improve the efficiency of function execution. Why not define all functions as inlining functions? If all functions were inline, would the keyword “inline” be necessary?

Inlining is at the expense of code bloat (copy) and simply eliminates the overhead of function calls, thus improving the efficiency of function execution. If the time spent executing the code inside the function is large compared to the overhead of the function call, the efficiency gains will be small. On the other hand, copying code for every inline function call increases the total amount of code in the program and consumes more memory.

Inlining should not be used when:

(1) If the code inside the function is long, the use of inlining will lead to a high cost of memory consumption.

(2) If there is a loop in the function body, then the time to execute the code in the function body is more expensive than the function call.

A good compiler will automatically disable undesirable inline lines based on the body of the function definition (this further explains that inline should not appear in a function declaration).

Conclusion:

Therefore, it is appropriate to implement inline functions in header files, saving you the trouble of implementing them once for each file. If the inline function is implemented once in every file, then it is better to ensure that every definition is the same. Otherwise, undefined behavior will result, meaning that if definitions are not the same in every file, which one the compiler expands depends on the particular compiler. Therefore, it is best to put the inline function definition in a header file.

Copyright Notice: This article was originally published BY CSDN blogger “ZQIxiao_09” under the CC 4.0 BY-SA copyright agreement. Original link: blog.csdn.net/zqixiao_09/…