answersLogoWhite

0


Best Answer

Inline expansion relates to the way in which a function call is replaced with the function body in your compiled code. The idea is that by eliminating the function call, your code will be faster because you eliminate the overhead of the function call (which requires a lot of unnecessary pushing and popping from the call stack). However, if the function is large and is called many times throughout your code, the extra speed gained by eliminating the function calls might be outweighed by the performance lost due to the increased code size.

Thus you will make a function inline when you know that there will be an advantage in doing so. In reality, however, marking a function for inline expansion merely states to the compiler that inline expansion is desired but is not required. The compiler is free to ignore the request when it can find no advantage in doing so. Some compilers may provide alternative methods that allow the programmer to overrule the compiler, but these should generally be avoided unless you can be absolutely certain there is an advantage to be gained (which will require an intimate understanding of your compiler's optimisers). In most cases it is best to let the compiler decide when inline expansion should occur, because what may be suitable for one program may not be suitable for all programs.

Aside from implementation-specific methods, there are essentially two ways to mark a function as a candidate for inline expansion: implicitly and explicitly. Implicit inline expansion occurs automatically whenever you define a function within its own declaration. Explicit expansion occurs when the declaration and definition are separate, but the definition is explicitly marked inline and appears in the same translation unit as the declaration. Where there is only one .cpp file, the definition may appear in that .cpp file (the declaration should always be placed in a header), but in all other cases this would result in an "unresolved external symbol" error from the linker. Thus the definition must appear in a header.

Note that although a definition is also a declaration, for the purpose of this answer a definition provides implementation while a declaration does not (a declaration in this sense simply means a forward declaration that will be implemented elsewhere).

The problem with inline expansion (whether implicit or explicit) is that the implementation may be visible to the caller, because it must also be visible to the linker. Whether this is desirable or not depends upon the function's purpose. However, the recommended method is to use explicit inline expansion, particularly with class methods. Although you cannot hide implementations within headers, you can limit the exposure by keeping the implementation physically separate. Consumers are only interested in the interface, so there's little point in confusing them with non-essential information within the declaration, which includes the use of the inline keyword (only the definition should be explicitly marked for inline expansion).

In most cases, inline expansion should be limited to functions that contain very simple statements or expressions. These can often result in reducing code size as well as improving performance. However, the same can be said of more complex functions where the compiler's optimisers can eliminate redundant code through procedural integration (making large functions smaller). Increased code size is not necessarily a bad thing unless the application is CPU-bound and the increased code size would result in increased paging (commonly known as thrashing). For non-CPU-bound applications inline expansion is largely irrelevant because overall performance would ultimately be determined by the bottlenecks elsewhere, such as file systems, databases or networks.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

6y ago

The simple answer is we never make a function inline. That is, we should never force a compiler or linker to inline expand. The reason is simply that we do not have enough relevant information to determine whether a function should be inline expanded or not (and if you have to ask the question, you are simply not qualified to make that determination). Even if we could calculate the pros and cons of inline expansion, those calculations would rendered moot the next time we modified the program. We've got far better things to do with our time -- especially when the compiler and linker can make those calculations for us every time we compile our program. That's their job, not ours.

Of course, many inline expansions are patently obvious to us as programmers, however if they are obvious to us then they are obvious to the compiler and linker, so it's really not something we ever have to worry about, particularly when the compiler and linker can interweave code in far more intricate ways besides inline expansion. Indeed, enforcing an inline expansion can interfere with other optimisations, leading to sub-optimal code

Contrary to belief, the inline keyword does not actually enforce inline expansion. The inline keyword is really only required whenever we define a function in a header because headers do not normally contain definitions (only declarations). If we do not declare the definition inline, and that header is subsequently included in multiple source files (something that we can't always predict in advance), then that definition is going to be recompiled wherever the file is included, leading to slower compile times. By declaring the definition inline, the compiler can inform the linker that this is the one and only definition and let the linker decide whether inline expansion is appropriate. Indeed, the linker is primarily responsible for most inline expansions because, unlike the compiler, which can only see the current translation unit, the linker can see the whole program and interweave code in far more intricate ways.

Some compilers (most notably Microsoft compilers) include non-standard extensions to the C++ language, including the force_inline keyword. Unlike the standard inlinekeyword, this really does impose an inline expansion. But because it can undermine compiler optimisations in ways that are extremely difficult to predict without in-depth knowledge of the compiler and linker implementations, this keyword is best avoided. Trial and error will quickly tell you that there is very little to gained by enforcing your will upon the compiler; let it do the job it was specifically designed to do.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When will you make a function inline and why?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is inline function in C Can you make inline function recursive or not If make can complier will compile that code?

The inline attribute is a C++ attribute, not a C attribute. Inline specifies that the function is to be expanded in place at the point of call instead of being called as a function. This means there will be one copy of the function for each call. This costs executable code, but can save execution time because the call setup and return time is avoided. Some functions cannot be inlined, and inline is really only a hint to the compiler. As far as recursive inlined functions, that depends on the implementation. The Microsoft implementation will not inline recursive functions unless they have a #pragma inline depth(n) line that specifies the maximum recusion depth the function will have. Consult your specific compiler documentation for the inline attribute for your specific implementation details.


What happens if recursion function is declared inline?

An inline function replaces the call to the function by the body of the function, thus reducing the overhead of saving the context in stack. This is good for functions which are small in size and called occasionally. A recursive function calls an instance of itself and thus can be a deeply nested. Different compilers handle this differently. Some will inline it up to a certain depth and then call a non-inlined instance for further recursion; others will not inline the function at all and generate a normal function call.


When will you make inline function?

Trivial functions, such as member variable accessors that simply return a member's value, are prime candidates for inline expansion. However trivial non-member functions can also be inline expanded, as can any non-trivial function that is rarely called.Member functions defined in the body of the class declaration are implicitly declared inline. However, whether a function is explicitly declared inline or not, the compiler is free to ignore any inline request, such as when the inline expansion of a non-trivial function would adversely compromise code size, for instance.Note that inline expansion replaces the call to a function with a modified version of the function's body within the calling functions -- just as if you'd duplicated the code yourself, rather than creating a separate function -- which removes the overhead of making a function call.The only way to force a function inline is to manually write the expanded code yourself. But if the code appears in several places, maintenance of the code will be compromised.If there's ever any doubt, declare it inline and let the compiler decide. It's in a far better position to determine if it should be inline expanded or not.


What is outline function in c plus plus language?

Outline is the opposite of inline. An inline expanded function is any function or class method where the declaration also provides the definition (the implementation). This is known as implicit inline expansion. Where the definition is kept separate from the declaration, you may use the inline keyword to specifiy that the function should be inline. This is known as explicit inline expansion. Inline expanded functions (whether implied or explicit) does NOT mean the function will in fact be inline expanded. It is merely a suggestion to the compiler. If the compiler's optimisers determine that there is no advantage to be gained by inline expanding a particular function, then that function becomes an outline function. Inline expansion simply means that the body of the function is inserted in place of the function call. Function calls are expensive in terms of memory and performance, so by eliminating the function call completely, your code performs faster and uses less memory. However, functions that are called many times throughout your code will result in a much larger code size, and large code runs slower than small code. Thus the benefit of eliminating a function call has to be weighed against the increased code size. Although some compilers do allow you to override the compiler's inline expansion optimisers, this is strictly non-standard. The best judge of what to expand and what not to expand is best left in the hands of the compiler, and indiscriminate use of the inline keyword should generally be avoided.


What are the rules for inline functions in c plus plus?

When you mark function as inline compiler puts the whole body of function in those places it is called, similar idea as in macros. If you do not mark function as inlinecompiler inside still decides which functions should be inline and which not. Inline function is less performance costly especially if function is called very often. Why it is lest performance costly? Because to invoke function you need to prepare parameters, put them to stack, make jump and etc. and all those steps are eliminated if function is inline.Example (very basic):inline int sum(int a, int b) {return a + b;}int c, d;c = sum(2, 3); /* compiler will change to 2 + 3 */d = sum(2, 5); /* this one will be changed to 2 + 5 */Full inline functions are allowed in ANSI/ISO C99.

Related questions

When will you make a function inline in c plus plus?

yes,we can make function inline


Which keyword is used to make function call faster in c?

Inline Function


What is inline function in C Can you make inline function recursive or not If make can complier will compile that code?

The inline attribute is a C++ attribute, not a C attribute. Inline specifies that the function is to be expanded in place at the point of call instead of being called as a function. This means there will be one copy of the function for each call. This costs executable code, but can save execution time because the call setup and return time is avoided. Some functions cannot be inlined, and inline is really only a hint to the compiler. As far as recursive inlined functions, that depends on the implementation. The Microsoft implementation will not inline recursive functions unless they have a #pragma inline depth(n) line that specifies the maximum recusion depth the function will have. Consult your specific compiler documentation for the inline attribute for your specific implementation details.


What happens if recursion function is declared inline?

An inline function replaces the call to the function by the body of the function, thus reducing the overhead of saving the context in stack. This is good for functions which are small in size and called occasionally. A recursive function calls an instance of itself and thus can be a deeply nested. Different compilers handle this differently. Some will inline it up to a certain depth and then call a non-inlined instance for further recursion; others will not inline the function at all and generate a normal function call.


Is inline function a command?

No, functions (inline or other) aren't commands.


When will you make inline function?

Trivial functions, such as member variable accessors that simply return a member's value, are prime candidates for inline expansion. However trivial non-member functions can also be inline expanded, as can any non-trivial function that is rarely called.Member functions defined in the body of the class declaration are implicitly declared inline. However, whether a function is explicitly declared inline or not, the compiler is free to ignore any inline request, such as when the inline expansion of a non-trivial function would adversely compromise code size, for instance.Note that inline expansion replaces the call to a function with a modified version of the function's body within the calling functions -- just as if you'd duplicated the code yourself, rather than creating a separate function -- which removes the overhead of making a function call.The only way to force a function inline is to manually write the expanded code yourself. But if the code appears in several places, maintenance of the code will be compromised.If there's ever any doubt, declare it inline and let the compiler decide. It's in a far better position to determine if it should be inline expanded or not.


When make a function inline?

Trivial functions, such as member variable accessors that simply return a member's value, are prime candidates for inline expansion. However trivial non-member functions can also be inline expanded, as can any non-trivial function that is rarely called.Member functions defined in the body of the class declaration are implicitly declared inline. However, whether a function is explicitly declared inline or not, the compiler is free to ignore any inline request, such as when the inline expansion of a non-trivial function would adversely compromise code size, for instance.Note that inline expansion replaces the call to a function with a modified version of the function's body within the calling functions -- just as if you'd duplicated the code yourself, rather than creating a separate function -- which removes the overhead of making a function call.The only way to force a function inline is to manually write the expanded code yourself. But if the code appears in several places, maintenance of the code will be compromised.If there's ever any doubt, declare it inline and let the compiler decide. It's in a far better position to determine if it should be inline expanded or not.


Difference between normal function inline function?

gffg


What is the difference between an outline and inline?

For the inline functions compiler just copies the function code in that place and when the size is too big it treats that function as ordinary function.


What is outline function in c plus plus language?

Outline is the opposite of inline. An inline expanded function is any function or class method where the declaration also provides the definition (the implementation). This is known as implicit inline expansion. Where the definition is kept separate from the declaration, you may use the inline keyword to specifiy that the function should be inline. This is known as explicit inline expansion. Inline expanded functions (whether implied or explicit) does NOT mean the function will in fact be inline expanded. It is merely a suggestion to the compiler. If the compiler's optimisers determine that there is no advantage to be gained by inline expanding a particular function, then that function becomes an outline function. Inline expansion simply means that the body of the function is inserted in place of the function call. Function calls are expensive in terms of memory and performance, so by eliminating the function call completely, your code performs faster and uses less memory. However, functions that are called many times throughout your code will result in a much larger code size, and large code runs slower than small code. Thus the benefit of eliminating a function call has to be weighed against the increased code size. Although some compilers do allow you to override the compiler's inline expansion optimisers, this is strictly non-standard. The best judge of what to expand and what not to expand is best left in the hands of the compiler, and indiscriminate use of the inline keyword should generally be avoided.


Is inline functions inform your compiler to optimize calls to the function?

No. The inline keyword simply tells the compiler that the function is a candidate for inline expansion. If the compiler's optimisers approve inline expansion, the function body is inline expanded at each call site, thus completely eliminating the overhead of the function calls at the expense of increased code size. If the increased code size would be detrimental to performance, the inline expansion is ignored completely. Note that functions that are defined within their own declarations are implicitly marked for inline expansion, thus the inline keyword should only be used where interfaces are declared separately (usually in header files) from their implementations (usually in source files). Also note that inline expansion is only suitable for small functions with one or two simple statements at most, or larger functions that are seldom called. Recursive functions can also be inline expanded, however the compiler will limit the depth of the calls. Any subsequent recursions will be treated as being standard function calls. However, most compilers also make use of tail recursion optimisers to minimise call depths.


What are the rules for inline functions in c plus plus?

When you mark function as inline compiler puts the whole body of function in those places it is called, similar idea as in macros. If you do not mark function as inlinecompiler inside still decides which functions should be inline and which not. Inline function is less performance costly especially if function is called very often. Why it is lest performance costly? Because to invoke function you need to prepare parameters, put them to stack, make jump and etc. and all those steps are eliminated if function is inline.Example (very basic):inline int sum(int a, int b) {return a + b;}int c, d;c = sum(2, 3); /* compiler will change to 2 + 3 */d = sum(2, 5); /* this one will be changed to 2 + 5 */Full inline functions are allowed in ANSI/ISO C99.