Getting Started with "go": About inline functions (inline) in C + +

Source: Internet
Author: User
Tags function prototype

Original address: http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/563879.html

In C + +, in order to solve some of the frequently called small functions to consume a lot of stack space or is called stack memory problem, especially introduced the inline modifier, expressed as an inline function.

Probably speaking here, many people still do not understand what is the stack space, in fact, the stack space refers to the local data of the program is the memory space of the function, in the system, the stack space is limited , if the frequent use of a large amount of space caused by the lack of stack of program error problems, The end result of the dead loop recursive invocation of the function is to cause the stack memory space to dry up.

Let's look at an example:

#include <Iostream>
#include <String>
Usingnamespace Std;

InlineString Dbtest (int a);function prototype declaration as inline: inline function


void main ()  

     for  (int i=1;i<=10;i++)  
    { 
        cout << i <<  ":"  << dbtest (i)  << endl; 
    } 
    cin.get (); 
 

 
string dbtest ( int a) //there is no need to inline again, and of course inline is not error  

    < Span style= "color: #0000ff;" >return  (a%2>0)? " Odd ":" Even "; 
 
}

The above example is the use of the standard inline function, the benefits of using inline modification we do not see the surface, in fact, in the internal work is in each for loop inside all calls Dbtest (i) place is replaced by (i%2>0)? " Odd ":" Even "this avoids the frequent calls to the function of the stack memory duplication of the cost of opening up.

Speaking of which many people may ask, since the inline is so good, it is better to declare the so-called functions as inline, well, the problem is to note that the use of the inline is limited, the inline is only suitable for function in the body code simple function Use, You cannot include complex structure control statements such as while switch, and you cannot inline the function itself without being a direct recursive function (you also call your own function internally).

Speaking of which, we have to talk about a # define statement that is widely used in C, yes define can do the work of inline, but define is a side effect, especially for errors caused by different types of parameters, This shows that inline has more constraints and features that allow the compiler to check for more errors, and it is not recommended to use define in C + +.

More examples of inline functions I do not cite, the flexible use of the learner itself, I just give a tip, so that you learn as much as possible in C + + some of the new advanced features of knowledge points.

Getting Started with "go": About inline functions (inline) in C + +

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.