C + + inline functions

Source: Internet
Author: User
Tags function definition microsoft c

In a program, a function call takes a certain amount of time and space (to protect the site) overhead. When we call small functions frequently, the cost of the function calls is relatively large (calling complex functions), so we need the same as the C language macro definition function (pre-compile will have a macro expansion), these small function code (at compile time) copied to the caller, To reduce the cost of function calls. to do this, we need to put a keyword inline in front of the function, telling the compiler that the function is an inline function that requires special handling. However, different compilers handle inline functions differently, possibly removing inline features from functions that add the inline keyword, whereas functions that the compiler considers to be inline but not prefixed with the inline keyword are handled inline.

The following details are excerpted from the blog what is C + + inline functions?

   in C, we have used Macro function A optimized technique used by compiler to reduce the execution time etc. So Question, comes in mind, what's there in C + + for, and in what's all better ways?Inline function is introduced which is a optimization technique used by the compilers especially to reduce the execut Ion time.We'll cover "What, Why, when & how" of the inline functions.

What is inline function:
The inline functions is a C + + enhancement feature to increase the execution time of a program. Functions can instructed to compiler to make them inline so, compiler can replace those function definition Whereve R those is being called. Compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.
Note-this is just a suggestion to compiler to make the function inline, if function was big (in term of executable instruc tion etc) Then, compiler can ignore the "inline" request and treat the function as normal function.

How to make function inline:
To do any function as inline and start its definitions with the keyword "inline".

1 Class A2 {3 Public :4InlineintAddintAintb)5     {6        return(A +b);7     };8 }9 Ten Class A One { A Public : -     intAddintAintb); - }; the  -InlineintA::add (intAintb) - { -    return(A +b); +}

   Why to use–
In many places we create the functions for small work/functionality which contain simple and less number of executable ins Truction. Imagine their calling overhead each time they is being called by callers.
When a normal function call instruction was encountered, the program stores the memory address of the instructions Immediat Ely following the function call statement, loads the function being called into the memory, copies argument values, jumps To the memory location of the called function, executes the function codes, stores the return value of the function, and T Hen jumps back to the address of the instruction, was saved just before executing the called function. Too much run time overhead.
The C + + inline function provides an alternative. With inline keyword, the compiler replaces the function call statement with the function code itself (process called Expan Sion) and then compiles the entire code. Thus, with inline functions, the compiler does not has the to-jump-another location-to-execute function, and then jump Back as the code of the called function was already available to the calling program.
With below pros, cons and performance analysis, you'll be able to understand the ' why ' for inline keyword
Pros:-
1.It speeds up your program by avoiding function calling overhead.
2.Itsave overhead of variables Push/pop on the stack, when function calling happens.
3.Itsave overhead of return call from a function.
4.It increases locality of reference by utilizing instruction cache.
5.By marking it as inline, you can put a function definition in a header file (i.e. it can is included in multiple Compila tion unit, without the linker complaining)

Cons:-
1.It increases the executable size due toCode Expansion.
2.c++ inlining is resolved at compile time. Which means if you change the code of the inlined function, you would need toRecompileAll the code using it to make sure it'll be updated
3.When used in a header, it makes your header file larger with information which users don ' t care.
4.As mentioned above it increases the executable size, which may cause thrashing in memory. More number of page fault bringing-your program performance.
5.Sometimes not useful for example in embedded system where large executable size was not preferred @ all due to memory co Nstraints.

When to use -
Function can be made as inline as per programmer need. Some useful recommendation is mentioned below-
1. Use the inline function when the performance is needed.
2.Use the inline function over macros.
3. Prefer to use inline keyword outside the class with the function definition to hide implementation details.

Key Points-
1.It ' s just a suggestion not compulsion. Compiler may or may not be inline the functions you marked as inline. It may also decide to-inline functions not marked as inline at compilation or linking time.
2.Inline works like a copy/paste controlled by the compiler, which are quite different from a pre-processor macro:the MACR O'll be forcibly inlined, would pollute all the namespaces and code, won ' t is easy to debug.
3.All the member function declared and defined within class is Inline by default. So no need to define explicitly.
4.Virtual methods is not supposed to be inlinable. Still, sometimes, when the compiler can know for sure the type of the object (i.e. the object is declared and constructed Inside the same function body), even a virtual function would be inlined because the compiler knows exactly the type of th E object.
5.Template methods/functions is not always inlined (their presence in an header would not make them automatically inline).
6.Most of the compiler would do in-lining for recursive functions But some compiler provides #pragmas-
Microsoft C + + compiler-inline_recursion (on) and once can also control their limit with inline_depth.
In GCC, you can also pass this in from the command-line with--max-inline-insns-recursive

C + + inline functions

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.