Understanding and using of C + + programming Squadron inline functions _c language

Source: Internet
Author: User
Tags function definition

Function call procedure
C + + compiled to generate executable program file exe, stored in the external memory. The program starts, the system loads the executable file into memory from the external memory, and executes from the entry address (at the beginning of the main function). When a call to another function is encountered in the execution of the program, the execution of the current function is paused, and the address of the next instruction is saved as the entry point to continue execution after the function is returned. Then go to the entry address of the adjusted function to perform the called function. After the return statement is encountered, or when the function is called, resumes the previously saved scene, continuing the rest of the melody from the previously saved returned address.

inline function
function calls require field protection to continue after a function call. After the function call, you need to restore the scene to continue. This requires system overhead and affects the efficiency of the program.

The inline function embeds the called function code directly into the calling function at compile time, which is defined by adding inline to the normal function definition, no program flow jump and return, but adding program code. The function body of an inline function cannot contain complex structure control statements and is suitable for small functions of 1-5 rows. When the function scale is bigger, the function time is relative to the function's invocation and the return time is much, the synthesis time and the space consideration, uses the inline to have not much significance.

Principle:
For any inline function, the compiler puts a function declaration (including first name, parameter type, return value type) in the symbol table. If the compiler does not find any errors in the inline function, the code for the function is also placed in the symbol table. When an inline function is invoked, the compiler first checks to see if the call is correct (type security check, or automatic type conversion, of course, all functions are the same). If correct, the code for the inline function replaces the function call directly, eliminating the overhead of the function call.

The difference between an inline function and a macro
1. Inline functions can be debugged at run time, while macro definitions are not possible;

2. The compiler will do security checks or automatic type conversions (with normal functions) on the parameter types of the function, while the macro definition will not;

3. The inline function can access the member variables of the class, and the macro definition cannot;

4. Declare a member function defined at the same time in a class and automatically convert to an inline function.

The function-inline mechanism of C + + language has both the efficiency of macro code and security, and it can manipulate the data members of the class freely. So in C + + programs, you should replace all the macro codes with inline functions

A function in the CPP file of an executable file can only be defined once. If you define a function in a. h file and have two CPP inclusions, this function will be defined in two CPP to produce an error. However, the inline function is allowed to be defined more than once in multiple CPP, which solves the problem.

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

The call to size () is actually inline. When looping, you can use variables to save the value of v.size () to reduce the call expense for Each loop. So decided a search, incidentally summed up.

1, inline of the derivation

Consider the following min () function

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

The benefits of defining a function for such a small operation are:
A. If a section of code contains a call to min (), it is much more readable to read such code and explain its meaning than to read an instance of a conditional operator.

B. It's much easier to change a localized implementation than to change the 300 occurrences in an application

C. Semantics are unified, and each test guarantees the same way to achieve

D. Functions can be reused without rewriting the code for other applications

However, there is a serious drawback to writing min () as a function: calling a function is much slower than directly calculating the condition operator. How does that take into account the above advantages and efficiency? C + + provides solutions for inline (inline) functions

2, the principle of inline: code substitution

At compile time, the compiler replaces the invocation expression of the inline function that appears in the program with the function body of the inline function.

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

int minVal2 = min (i, j); 

is expanded at compile time to

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

The extra execution cost of writing min () into a function is eliminated.
3, the use of inline

Let a function become an inline function, implicitly define the function in the class, and explicitly precede the function with the inline keyword description.

4, the use of inline some points of attention

A. From the principle of inline, we can see that the principle of inline, is the use of space in exchange for time, is based on the Code expansion (replication) as the cost, only omitted the cost of function calls, so as to improve the efficiency of function execution. If the time of executing the code in the function body is greater than the overhead of the function call, then there will be little efficiency gains. Therefore, if the function body code is too long or the function weight has circular statements, if statements or switch statements or recursion, it is not appropriate to use inline

B. Keyword inline must be placed with the function definition body to make the function inline, and it does not have any effect until the inline is placed in front of the function declaration. Must be declared before an inline function call.

inline void Foo (int x, int y); Inline only with function declarations the 
void Foo (int x, int y) 
{ 
  ... 
} 

The above code cannot be an inline function, and the following can be

void Foo (int x, int y); 
inline void Foo (int x, int y)//inline with the function definition body 
{ 
  ... 
} 

So, inline is a "keyword for implementation", not a "keyword for declaration". For the above example, Lin Rui also suggests adding inline to the definition, rather than the declaration and definition, because it embodies a fundamental principle of high quality C++/C programming style: Declaration and definition are not to be confused.
C.inline is just a recommendation for the compiler, and the compiler can choose to ignore the recommendation. In other words, even if the inline is really written, and without any errors, the compiler will automatically optimize. So when recursion, loops, or too much code appears in the inline, the compiler automatically ignores the inline declaration, as well as the normal function call.


in summary:

Think that inline can be understood as a function-specific macro in C + +, an improvement to the function macros of C. For Chang, C + + provides const substitution, whereas for function macros, C + + provides a solution that is inline. In C, we all know the advantages of the macro, the compiler through the duplication of macro code, eliminating the parameters of the stack, generate assembly call calls, return parameters and other operations, although there are some security risks, but in terms of efficiency, it is very desirable.
However, there are a number of shortcomings in the function macro, mainly the following:

A. When copying code, there is an unexpected marginal effect, such as the classic

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

In the EXECUTE statement:

result = MAX (i, J) + 2; 

, it will be interpreted as

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

B. Using macros, debugging cannot be done, although Windows provides an Assert macro
C. Using macros, you cannot access private members of a class
Therefore, C + + through the inline mechanism, not only with the efficiency of macro code, but also increased security, you can freely manipulate the class of data members, is a relatively perfect solution.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.