C ++ inline functions

Source: Internet
Author: User

C ++ inline functions
In C language, we use macro-defined functions and compiler optimization technology to reduce program execution time. Are there any similar technologies or better implementation methods in C ++? The answer is yes, that is, inline functions. Inline functions are a technique used in Compiler Optimization to reduce the running time. We will introduce inline functions from the following four aspects: What are inline functions? Why are inline functions used? Advantages and Disadvantages of inline functions? How to Use inline functions. What is an inline function? an inline function is one of the enhancements of C ++ to reduce the running time of a program. When an inline function receives an instruction from the compiler, it can be inline: the compiler uses the definition body of the function to replace the function call statement. This substitution behavior occurs in the compilation phase rather than the program running stage. It is worth noting that the inline function is only an inline suggestion for the compiler. Whether the compiler feels that your suggestion depends on whether the function meets the inline favorable conditions. If the function body is very large, the compiler will ignore the inline Declaration of the function and treat the inline function as a normal function. When defining a function inline, declare the function with the keyword "inline" at the beginning of the function, so that the function is called inline declaration function. Example: Class A {Public: inline int add (int a, int B) {return (a + B) ;}}class A {Public: int add (int, int B) ;}; inline int A: add (int a, int B) {return (a + B );} why do we need to use inline functions? Sometimes we write some function-specific functions. These functions are not very specific and contain very few execution statements. For example ~ When the prime number is less than 1000, we often use the open side operation to narrow the operation range. Then we will write a function:

int root(int n){  return (int)sqrt((float)n);}

 

Then we can write the function for finding the prime number in the range.
int prime(int n){    int i;    for (i = 2; i <= root(n); i++)    {        if (n%i == 0)      return 0;        return 1;    }}            

 

Of course, placing the root function in a loop is not an unwise choice, but imagine that a function similar to root must be called frequently in a program context, how much does it cost to call a function: When a common function call command is executed, the program will save the current function execution site and press the local variables and function addresses in the function into the stack, then, load the new function to be called into the memory, which involves copying parameter values, redirecting to the memory location of the called function, executing the function code, and storing the function return value, after the function is executed, obtain the address of the previously called function and continue executing the function. The running time overhead is too high. C ++ inline functions provide a solution to replace function calls. With the inline statement, the compiler replaces the function call statement with the function body statement in the function call, then compile the replaced code. Therefore, through inline functions, the compiler does not need to jump to other memory addresses to execute function calls, nor need to retain the field data during function calls. The advantages and disadvantages of inline functions are summarized below to better understand why the inline function is used: advantages: it increases the running speed of your program by avoiding the overhead caused by function calls. When a function is called, it saves the overhead of variable stack and pressure stack. It avoids the overhead of returning the original site after a function is executed. By declaring a function as inline, you can put the function definition in the header file. Disadvantage: Due to code extension, inline functions increase the size of executable programs. The expansion of the C ++ inline function is the compilation phase, which means that if your inline function is changed, you need to re-compile the code. When you put the inline function in the header file, it will increase your header file information, but the header file users do not need to care about this. Sometimes inline functions are not favored. For example, in embedded systems, the storage constraints of embedded systems may not allow large executable programs. When should I use inline functions? Every function can be declared as inline when a program is designed. Some useful suggestions are as follows: when there are requirements on program execution performance, use inline functions. When you want to define a macro function, use the inline function. Functions defined in the class are declared as inline functions by default, which is helpful for hiding the class implementation details. The key point inline declaration is just a kind of advice to the compiler. It is up to the compiler to decide whether to adopt inline measures. Even in the assembly or link phase, some function compilers without inline declarations will expand it inline. The Inline of the compiler looks like copying and pasting code, which is different from the preprocessing macro: the macro is forced to expand inline, which may pollute all namespaces and code, it will bring difficulties to program debugging. All functions defined in the class are declared as inline functions by default, and we do not need to declare inline explicitly. Inline is not allowed for virtual functions. Although the template functions are put in the header file, they are not necessarily inline. (Not to say that all functions defined in the header file are inline functions ).

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.