Understanding and use of C++ programming squadron inline functions

  • 2020-05-09 18:52:05
  • OfStack

Function call procedure
c++ is compiled to generate the executable program file exe, which is stored in external storage. The program starts, the system loads the executable file into memory from external storage, and execution begins at the entry address (the beginning of the main function). When other functions are called in the execution of the program, the execution of the current function will be suspended, and the address of the next instruction will be saved as the entry point to continue the execution after the return of the called function, so as to save the scene. Then go to the entry address of the called function to execute the called function. After the return statement or the end of the called function is encountered, the previously saved locale is restored and the rest of the main function is continued from the previously saved return address.

Inline function
The function call needs field protection to continue after the function call. After the function is called, you need to restore the field to continue execution. This requires overhead and affects the efficiency of the program.

Inline functions are directly embedded in the calling function code in the main call function at the time of compilation. The definition method is to add inline before the ordinary function definition. There is no program flow jump and return, but the program code is added. Inline functions the body of a function cannot contain complex structural control statements. It is suitable for small functions on lines 1-5. When the size of the function is relatively large, the running time of the function is much larger than the call and return time of the function. Considering time and space, inlining does not make much sense.

Principle:
For any inline function, the compiler places the declaration of the function (including name, parameter type, return value type) in the symbol table. If the compiler does not find an error in an inline function, the code for that function is also placed in the symbol table. When calling an inline function, the compiler first checks to see if the call is correct (either type safety checking, or automatic type conversion, of course, for all functions). If correct, the inlined function code will replace the function call directly, eliminating the overhead of the function call.

The difference between inline functions and macros
1. Inline functions can be debugged at run time, but macro definitions cannot;

2. The compiler will perform security checks or automatic type conversion on the parameter types of inline functions (same as normal functions), while macro definitions will not.

3. Inline functions can access the member variables of the class, while macro definitions cannot.

4. Declare the member functions defined in the class at the same time, and automatically convert them to inline functions.

The function inline mechanism of C++ provides the efficiency of macro code, added security, and the freedom to manipulate the data members of a class. So in an C++ program, you should replace all macro code with inline functions

A function in the cpp file of an executable can only be defined once. If you define a function in a.h file and include two cpp files, you will get an error when the function is defined in two cpp files. However, the inline function is allowed to be defined multiple times in multiple cpp, which solves this problem.


for (int i=v.begin() ; i<v.size() ; i++) 
{ 
  .... 
} 

           's call to size() is actually inline. During the loop, you can save the value of v.size () in a variable to reduce the invocation overhead per loop. So decided to 1 search, by the way summary of it.

1. Introduction of inline

Consider the following min() function


int min( int v1, int v2 ) 
{ 
  return( v1 < v2 << v1 : v2 ); 
} 

          the advantages of defining a function for such a small operation are:
If a piece of code contains a call to min(), it is much more readable to read the code and explain what it means than to read an instance of a conditional operator.

        b. It is much easier to change one localized implementation than to change 300 occurrences in one application

        c. The semantics are unified and each test ensures the same implementation

        d. Functions can be reused without having to rewrite the code for other applications

However, there is one serious drawback to writing min() as a function: calling the function is much slower than directly evaluating the condition operator. How can you balance the above advantages and efficiency? The solution provided by C++ is the inline (inline) function

2. Principle of inline: code substitution

            when the program is compiled, the compiler replaces the call expression of the inline function in the program with the function body of the inline function.

            for example, if a function is specified as inline it will be expanded inline at each call point in the program for example


int minVal2 = min( i, j ); 

At compile time is expanded to


int minVal2 = i < j << i : j; 

  eliminates the extra execution overhead of writing min() as a function.
3. Use of inline

            makes a function inline, implicitly defining the function in the class, and explicitly adding the inline keyword to the function.

4. Some tips for using inline

From the principle of           a, we can see that the principle of inline is to trade space for time, at the cost of code bloat (copy), only save the overhead of function call, so as to improve the efficiency of function execution. If the time it takes to execute the code inside the function is more expensive than the function calls, the efficiency gains are small. So, if the function body code is too long or the function weight has a loop, if or switch or recursion, inlining is not appropriate

          b. The keyword inline must be placed with the body of the function definition to make the function inline. Inline function calls must be declared before they are called.


inline void Foo(int x, int y); // inline  Place only with function declarations 1 since  
void Foo(int x, int y) 
{ 
  ... 
} 

The above code cannot be inline, but the following can


void Foo(int x, int y); 
inline void Foo(int x, int y) // inline  And the body of the function definition 1 since  
{ 
  ... 
} 

            so inline is a "keyword for implementation", not a "keyword for declaration". For the above example, Lin also suggests adding inline only before definition, not before declaration and definition, because this can reflect a basic principle of high quality C + + / C programming style: declaration and definition cannot be mixed into 1.
            c.inline is just one recommendation for the compiler, which the compiler can choose to ignore. In other words, if inline is actually written without any errors, the compiler will automatically optimize it. So when there is recursion, loops, or too much code in inline, the compiler automatically ignores the inline declaration, again as a normal function call.


Summary:

            felt that inlining could be understood as a function specific macro in C++ and an improvement on C function macros. For constant macros, C++ provides const substitution; For function macros, C++ provides inline. In C, we all know the advantages of macros. The compiler can copy the macro code in such a way as to save the parameter stack, generate the call call of assembly, and return the parameters. Although there are some security risks, it is still desirable in terms of efficiency.
           

            a. When copying code, it is easy to have unexpected marginal effects, such as the classic


#define MAX(a, b) (a) > (b) ? (a) : (b) 

In the execution statement:


result = MAX(i, j) + 2 ; 

, will be interpreted as


result = (i) > (j) ? (i) : (j) + 2 ; 

        b. Cannot be debugged using macros, although windows provides ASSERT macros
        c. Using macros, private members of the class cannot be accessed
          so, C++ through the inline mechanism, not only has the efficiency of macro code, but also increases the security, can also operate the data members of the class, is a relatively perfect solution.


Related articles: