Inline functions (Inline) in C ++ ))

Source: Internet
Author: User

In C ++, inline modifiers are introduced to solve the problem that frequently called small functions consume a lot of stack space or stack memory.

Many people may not understand what stack space is. In fact, stack space refers to placement.ProgramThe local data is the memory space of the data in the function. In the system, the stack space isLimitedIf a large number of applications are frequently used, program errors may occur due to insufficient stack space. The final result of the endless recursive call of functions is the depletion of stack memory space.

Here is an example:

# Include <iostream>
# Include <string>
Using namespace STD;

Inline string dbtest (int A); // the original function declaration is inline: Inline Function


Void main ()
{
For (INT I = 1; I <= 10; I ++)
{
Cout <I <":" <dbtest (I) <Endl;
}
Cin. Get ();

}

String dbtest (int A) // you do not need to use inline again here. Of course, adding inline will not cause errors.
{
Return (a % 2> 0 )? "Odd": "even ";

}

The above example shows the usage of standard inline functions. We cannot see the benefits of inline modification, in fact, the internal work is to replace (I % 2> 0) Where dbtest (I) is called within each for loop )? "Odd": "even", this avoids the consumption caused by frequent calls to functions to repeat stack memory.

Many people may ask that since inline is so good, it is better to declare all the so-called functions as inline. Well, you should pay attention to this question, the use of inline is limited. inline is only applicable to function bodies.CodeSimple functions cannot contain complex structure control statements such as while switch, and inline functions cannot be directly recursive functions (they also call their own functions internally ).

Speaking of this, we have to talk aboutC LanguageThe # define statement is widely used. Yes, define can also do the work of inline, but define will produce side effects, especially errors caused by different types of parameters, it can be seen that inline is more restrictive and allows the compiler to check for more error features. Define is not recommended in C ++.

I will not give more examples of inline functions. Flexible use depends on the learners themselves, let everyone learn as many new advanced feature knowledge points in C ++ as possible.

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.