inline use

inline can be seen as a function of macro expansion

In c / c ++ in order to solve some small function called frequently consume a lot of problems stack space (stack memory), in particular the introduction of the inline modifier, expressed as an inline function, stack space means partial data placement program (also It is within the function data) memory space. Under the system, the stack space is limited, a lot of use if it will cause frequent due to lack of stack space program error caused problems, such as the death of a recursive loop function calls end result is to cause a stack memory space exhausted.

#include <stdio.h> 
#include < String .h> // function is defined as inline namely: inline function 
inline char * inline_test ( int NUM) 
{ return (NUM% 2 > 0 ?) " odd " : " even " ; 
} int main () 
{ int I = 0 ;
    for (I = . 1 ; I < 10 ; I ++ ) 
   { 
       the printf ( " inline_test: I: D parity%:% S \ n- "
 

    
 
 

   , i, inline_test(i));   
   }
   
   return 0;
}

dbtest (i) the place have been replaced with (i% 2> 0) "odd":? "even", thus avoiding the frequently called functions of the stack memory consumption caused by repeated open.

The use of inline there is a limit, the number of inline only suitable for in vivo Han Han number of simple codes used,
(1) not contain complex structures, for example, control statements while, switch, and can not inline function itself can not be directly recursive function (i.e. , also call their own internal functions).
(2) and all (except the most mundane, almost nothing to do) virtual function, chasing prevent inlining performed.
This should not cause much surprise, because virtual means "wait until run time and then determine which function to call",
and inline says it means "at compile time, the action will be called to replace the body of the called function . "
If the compiler to make a decision, do not yet know what the call a function, it is hard to oblige them to make a inline function.

[Reference blog] ( https://blog.csdn.net/u011857683/article/details/81606433 )

Guess you like

Origin www.cnblogs.com/lanclot-/p/11268937.html