About the usage of inline

Inline function

1. Definition
2. Differences between inline functions and other functions
3. Usage rules
4. Range of use
5. Differences between inline functions and macros
6. Advantages and disadvantages of inline functions

1. Definition

A function decorated with inline is called an inline function, and the C++ compiler will expand it at the place where the inline function is called during compilation. Its advantage is that there is no overhead of function pushing on the stack,
and the inline function improves the efficiency of program operation.

2. The difference between inline functions and other functions
1) Inline functions are directly copied and "mosaic" into the main function, that is, the code of the inline function is directly placed in the position of the inline function, which is different from the general function , When the main function calls a general function, the instruction jumps to the entry address of the called function. After the called function is executed, the instruction jumps back to the main function to continue executing the following code; and because the inline function The code of the function is directly placed in the position of the function, so there is no instruction jump, and the instructions are executed in order

2) There is only one copy of the code segment of the general function, which is placed in a certain position in the memory. When the program calls it, the instruction jumps over; when the program calls it the next time, the instruction jumps over again; while the inline function The inline function is called several times in the program, and the code of the inline function will be copied and placed in the corresponding position

3. Rules of Use

Generally, when using inline functions, you must write the inline keyword and the defined function body together to achieve inlining. If you just add the inlie keyword in front of the function, the effect of inlining cannot be achieved. for example

inline int add(int left,int right)
{
    
    
     return left+right);
}

int main()
{
    
    
     int a=10;
     int b=20;
     int c=0;
     c=add(a,b);
}

4. Scope of use
1) Inline is a practice of trading space for time, eliminating the overhead of calling functions. Therefore, functions with long code or loop/recursion are not suitable for use as inline functions. And it is not recommended to use it when it is called repeatedly.

2) Inline is just a suggestion for the compiler, and the compiler will automatically optimize it. If there are loops/recursion,
etc. in the function body defined as inline , the compiler will ignore the inline during optimization.
3) Inline is not recommended to separate declarations and definitions. Separation will lead to link errors. Because the inline is expanded, there is no function address, and the link will
not be found.
Insert picture description here

5. About the difference between inline functions and macros.
The advantage of macro functions is that they will be expanded during the preprocessing stage, reducing the overhead of function calls (parameter transfer, parameter stacking and stack frame overhead)

Disadvantages: Replacement occurs in the preprocessing stage (preprocessing-compilation-assembly-link), it will not participate in compilation, and there is no type detection.

for example
Insert picture description here

The difference between the inline function and the macro function is that it is replaced during the compilation phase, which can solve the problem in the above example.

Therefore, in C++, macro function definitions are replaced with inline functions.

6. The advantages and disadvantages of inline functions

Excellent: Avoid jumping back and forth of instructions, speed up program execution

Lack: The code has been copied multiple times, increasing the amount of code and taking up more memory space

Reference blog: https://blog.csdn.net/qq_33757398/article/details/81390151

Guess you like

Origin blog.csdn.net/qq_43530773/article/details/113783829