inline functions in C + + (inline)

Source: Internet
Author: User
Tags function prototype

In C + +, in order to solve some frequently called small function large consumption stack space or is called stack memory problem, especially the introduction of the inline modifier, expressed as an inline function.

Probably speaking here, many people still do not understand what is stack space, in fact, the stack space refers to the placement of the program's local data is the function of the data in the memory space, in the system, the stack space is limited, if the frequent use of a large number of stack space caused by the problem of the program error, The end result of a recursive call to the dead loop of a function is to deplete the stack memory space.

Let's take a look at the following example:

#include <iostream>
#include <string>
using namespace Std;
Inline string dbtest (int a); Function prototype declared as inline: inline function
void Main ()
{
for (int i=1;i<=10;i++)
{
cout << i << ":" << dbtest (i) << Endl;
}
Cin.get ();

}
string dbtest (int a)//no more inline here, of course, plus inline is not going to go wrong.
{
Return (a%2>0)? " Odd ":" I ";

}

The example above is the use of standard inline functions, the benefits of using the inline modification we do not see, in fact, the internal work is in each for loop inside all the calls dbtest (i) replaced by (i%2>0)? " Odd ":" I "this avoids the frequent call function to the stack memory repeatedly open up the consumption.

Speaking of this, many people may ask, since inline so good, it is better to put the so-called functions are declared as inline, well, this problem is to be noted that the use of inline is limited, inline only for functions in the body code simple function Use, Cannot contain complex structure control statements such as while switch, and cannot inline functions themselves cannot be direct recursive functions (their own internal also call their own functions).

Speaking of which we have to talk about the #define statement that is widely used in C language, yes define can do the inline work, but define will have side effects, especially the errors caused by different types of parameters. This shows that inline is more restrictive and can allow the compiler to check out more error characteristics, in C + + is not recommended to use define.

More examples of inline functions I do not cite one, flexible use also rely on the learner itself, I only here, so that we learn as much as possible in C + + some of the new advanced characteristics of knowledge points.

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.