C/C++ inline function inline usage summary notes record

The road is hindered and long, and the line is coming. Keep your head down and work hard, if you don't talk, you will be a blockbuster! Come on, Sao Nian!
Welcome to follow my WeChat public account: BabyCoder, I will provide you with more useful articles, thank you!

Reference

Reference URL

  [Blog Garden]: Inline usage in C++
  [Novice Tutorial]: Inline usage in C++ (reproduced from the above blog garden)

Reference book

  "C++ Primer Fifth Edition" page P213

  Most of the notes in this article can be found from the above information~

Self summary

Reasons for introducing inline function inline keyword

  • Mainly to solve some small function called frequently consume a lot of stack space (stack memory) problems;
  • Stack space: refers to the memory space where the program's local data (that is, the data in the function) is placed.
  • In the system, the stack space is limited, and frequent and large use will cause program errors due to insufficient stack space. For example: the end result of the infinite loop recursive call of the function is to cause the stack memory space to be exhausted.
  • Calling a function is generally slower than finding the value of an equivalent expression. On most machines, a function call actually involves a series of tasks: save the register before the call, and restore it when returning; it may be necessary to copy the actual parameters; the program moves to a new location to continue execution.

Inline function concept

  • In the function definitions / implemented at increasing inline modifier;
  • The member functions defined in the class are inline by default (default);

Features of inline functions

  • Inline functions can avoid the overhead of function calls;
  • Specify a function as an inline function (inline), which is usually expanded "inline" at each call point;
  • The introverted description is just a request to the compiler, and the compiler can choose to ignore this request;
  • Generally speaking, the inline mechanism is used to optimize functions that are small in scale, direct in process, and frequently called. Many compilers do not support inline recursive functions, and a 75-line function is unlikely to be expanded inline at the call site.

Inline function actual operation process

  • When we use inline to modify the function as an inline function, call this function in other places, and the specific implementation method of the function will be automatically replaced during compilation (analogous feature: inline expansion);
  • Therefore, the use of inline functions is at the cost of consuming memory space in exchange for program execution efficiency;

Summarize the content of the blog post

  • In the C language, the definition of inline functions is best placed in the header file. (Should be C language only)

  • inline is a "for implementing key"; that is inline must function definition body put together to make the function inline, before only on the function declaration has no effect.

  • The author believes that inline should not appear in the function declaration. Although it will not affect the function of the function, it embodies a basic principle of high-quality C/C++ programming style: declarations and definitions cannot be confused, and users do not need or should know whether functions need inlining.

  • Use inline with caution

    • Inline is code bloat (copy) the cost of just eliminating the function call overhead, thereby improving the efficiency of the function.
    • If the time to execute the code in the function body is larger than the cost of the function call, the efficiency gain will be small.
    • Every inline function call must copy the code, which will increase the total code size of the program and consume more memory space.
  • Inline should not be used in the following situations

    • If the function code in the body longer , in-use will result in higher cost of memory consumption .
    • If the function body appears cycle , the time to perform a function within the code of a function call overhead than big.
    • Class constructors and destructors can easily be misunderstood that using inlining is more effective. Beware constructor and destructor may hide some behaviors , such as the "secretly" performed base member or the object class constructor and destructor. It should not be defined in the constructor and destructor member functions in the class declaration .
  • Brief summary

    • Inline functions are not a panacea for performance enhancement.
    • Only when the function is very short when it can get the effect we want; however, if the function is not very short and in many places have been called, then will make the executable volume increases.
    • If inline functions do not enhance performance, avoid using them!

If the content of the article is wrong, please comment / private message a lot of advice, thank you! If you think the content of the article is not bad, remember to click three links (like, bookmark, leave a message), your support is my greatest encouragement, thank you!

Guess you like

Origin blog.csdn.net/Fighting_Boom/article/details/107909996