当前位置:网站首页>Keyword inline (inline function) usage analysis [C language]

Keyword inline (inline function) usage analysis [C language]

2022-07-06 11:58:00 Weiyuan escort agency

One 、 What is an inline function

        stay C In language , If some functions are called frequently , There are functions on the stack , Function stack , It will cause a large consumption of stack space or stack memory .

        To solve this problem , In particular, the introduction of inline Modifier , Expressed as an inline function .

        Stack space refers to the local data of the program, that is, the memory space of the data in the function , Under the system , Stack space is limited , If it is used frequently, it will cause the problem of program error caused by insufficient stack space , The end result of the function's dead loop recursive call is that the stack memory space is exhausted .
Let's take an example :

#include <stdio.h>  
 
// The function is defined as inline namely : Inline function   
inline char* dbtest(int a) 
{  
    return (a % 2 > 0) ? " p. " : " accidentally ";  
}   
  
int main()  
{  
    int i = 0;  
    for (i=1; i < 100; i++) 
    {  
        printf("i:%d     Parity :%s \n", i, dbtest(i));      
    }  
} 


      The above example is the use of the standard inline function , Use inline We can't see the benefits of decoration on the surface , In fact, the internal work is in every for Any call inside the loop dbtest(i) All the places in the hotel have been changed to (i%2>0)?" p. ":" accidentally " In this way, we can avoid the consumption of repeatedly opening stack memory by calling functions frequently .

      In fact, this kind of problem is a bit similar to the dynamic library and static library we learned earlier , send dbtest The code in the function is put directly into main Function , perform for loop , Will keep calling this code , Instead of constantly opening up a function stack .

Two 、 The programming style of inline functions

1、 keyword inline Must be placed with the function definition body to make the function inline , Only will inline Putting it before a function declaration doesn't work .

The following style of function Foo Can't be an inline function :

inline void Foo(int x, int y); // inline  Only with function declaration 
void Foo(int x, int y)
{
}

And the following style of function Foo It becomes an inline function :

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

        So ,inline It's a kind of “ Keywords for implementation ” , Not a kind of “ Keywords for declaration ”. In a general way , The user can read the function declaration , But you don't see the definition of a function . Although in most textbooks the declaration of inline functions 、 The definition body is prefixed with inline keyword , But I think inline It should not appear in the declaration of a function . This detail does not affect the function of the function , But it shows high quality C++/C A basic principle of programming style : Declaration and definition should not be confused , Users don't have to 、 You shouldn't know if a function needs to be inlined .

2、inline There are limits to the use of

      inline It is only suitable for functions with simple code in the body of culvert numbers , Cannot contain complex structure control statements such as while、switch, And the inline function itself cannot be a direct recursive function ( I also call my own functions inside ).

3、 ... and 、 Be careful with inline

        Inline can improve the efficiency of function execution , Why not define all functions as inline functions ? If all functions are inline functions , You can use it “ inline ” This keyword ?

        Inline is code bloat ( Copy ) At the cost of , It only saves the cost of function call , So as to improve the efficiency of function execution . If the execution time of the code in the function body , Compared with the cost of function call, it is more expensive , Then the efficient income
Little success . On the other hand , Every call to an inline function copies the code , Will increase the total code amount of the program , Consume more memory space .

Inline should not be used in the following situations :

(1) If the code in the function body is long , Using inline will result in high memory consumption cost .

(2) If there's a loop in the body of the function , Then it takes more time to execute the code inside the function than to call the function .

A good compiler will be based on the definition body of a function , Automatically cancel unworthy inlining ( This further explains inline It should not appear in the declaration of a function ).


summary :

        therefore , It is appropriate to implement inline functions in header files , Save you the trouble of implementing it once per file . So the declaration should be consistent with the definition , In fact, it means , If the inline function is implemented once in every file , that , It's better to make sure that every definition is the same , otherwise , Will cause undefined behavior , That is , If the definition is not the same in every file , that , Which one does the compiler expand , It depends on the compiler . therefore , It's better to put the inline function definition in the header file . 
 

原网站

版权声明
本文为[Weiyuan escort agency]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060913226586.html