answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

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

Wiki User

11y ago

Inline functions execute quicker because the code is inline expanded by the compiler, thus eliminating unnecessary function calls. Function calls are expensive in terms of both memory and performance, so the fewer function calls there are, the better.

However, there is a trade-off between fewer function calls and increased code size. Inline expansion results in duplicate code if a function is called from more than one place in your code and the increased size could easily offset any gains made by eliminating function calls. For this reason, the compiler is free to ignore a request for inline expansion if there is no advantage in doing so. Declaring a function as inline expanded is merely a hint to the compiler, nothing more. And although you are free to force the compiler to inline expand your functions, it is recommended you keep this to an absolute minimum. The compiler is the best judge on what functions should and should not be inline expanded.

For the same reason, it is not recommended that you manually expand your functions. Functions reduce the amount of duplicate code within your sources, and make it much easier to both read and maintain your code. Even if a function is only called once, if it makes your code easier to read then that is a good thing, and a function that is called only once is a good candidate for inline expansion anyway.

You should also avoid declaring too many inline functions as this can adversely affect compilation times. Deciding which functions are good candidates for inline expansion is not always straightforward as it's largely dependant upon how complex the function is and how often it is called. Generally speaking, you will implement simple functions at the point of declaration and these will be implicitly inline expanded so there is no need to declare them inline (the compiler is still free to ignore the request, however). For more complex functions where the implementation is typically separated from the declaration, inline expansion needs to be explicitly declared. However, you should only do this for complex functions that are seldom called. Some functions, such as recursive functions, cannot be inline expanded, even by force, so there's no point in trying. As a rule of thumb, if you cannot easily expand a function by hand, then there's simply no point in expecting the compiler to do it either.

Simple functions that are good candidates for inline expansion are those that have just one or two simple statements. Class get accessors are almost always good candidates for inline expansion since most can be implemented at the point of declaration.

Macro functions (and macros in general) should be avoided whenever possible. These are inline expanded whether you like it or not, but as they're not type safe and cannot be easily debugged, they are best avoided altogether. Template functions (and type-safe constants) are a far safer option, are much easier to work with and debug, and they can be inline expanded when it is beneficial to do so.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Functions make life easier for programmers, essentially allowing the same code to be re-used, over and over again, without having to duplicate that code wherever it is needed. However, function calls are expensive in terms of performance.

For functions that are seldom called, or that perform complex operations, this isn't a major problem. But for simple functions that do very little real work but are called often, performance could suffer. If the time it takes to actually set up a function call and return from that call is greater than the time it takes to actually perform the function itself, then it is clearly better to remove the function call altogether.

That's where inline functions come in. By declaring a function to be inline, you are signalling to the compiler that the function is to be inline expanded, replacing every function call with the actual code of the function. This gives you the best of both worlds -- there's still only one function to maintain and call, and the performance penalty of actually making a function call is removed completely.

However, declaring a function to be inline expanded is no guarantee it actually will be inline expanded. The compiler is free to ignore the request altogether. This is simply because inline expansion increases executable size but also because not every function is a good candidate for inline expansion. The compiler is in a better position to decide which functions will be inline expanded, but you are free to include functions that might otherwise have been ignored by the compiler.

Functions that are defined within their own declarations are automatic candidates for inline expansion, but class member accessors are also prime candidates since most do nothing other than return a constant reference or value.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

An inline function gives you all the benefits of declaring a function, but without the overhead of an actual function call.

To understand what an inline function is, consider the following code:

#include<iostream>

int sum(int a, int b){ return(a+b); }

int main()

{

int x=40, y=2;

int z=sum(x,y);

return(0);

}

The sum() function is an ideal candidate for inline expansion because its implementation is simple and it is seldom called (in this case it is only called once). The compiler can effectively remove the function call completely simply by replacing the function call with the equivalent inline expanded code:

int main()

{

int x=40, y=2;

int z=x+y;

return(0);

}

While the above example is trivial and the original function call was obviously superfluous, inline expansion comes into its own when the function implementation is relatively simple but is called often, throughout your program. Although the inline expansion will result in a larger code size, eliminating those function calls will improve the performance of those calls, and you still have the benefit of having the function in the event you wish to modify its implementation at a later date. Had you manually expanded the code yourself, as shown above, then you'd be forced to make the same modifications in several places in your code, which not only increases the maintenance costs, but can easily lead to errors should you forget to propagate your changes correctly. By keeping re-usable code in one place, it is much easier to maintain, so you gain the benefit of having the function, without the expense of a function call. Inline functions can also be used to reduce complex functions to a series of simpler inline function calls, thus making your code that much easier to read and maintain but without compromising performance in any way.

It should be noted that declaring a function inline is merely a hint to the compiler. There is no guarantee that a function will be inline expanded if the resultant increase in code size would be deemed more detrimental to performance than any benefits gained by eliminating the function calls. Certain functions, particularly recursive functions where the depth cannot be determined at compile time, or where the depth would exceed 16 recursions, cannot be inline expanded. However, simple functions, such as class accessors, or functions that are seldom called are often ideal candidates for expansion.

It should also be noted that a function call is either inline expanded or it is not. You cannot choose to inline expand some calls but not others to the same function.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

Inline functions must be small functions, usually with no more than one or two simple statements. Inline functions must not be recursive, although some compilers will permit inline recursion up to a certain depth (the VC++ maximum is 16). The inline keyword may be applied to the declaration of the function, the definition, or both. However, marking a function for inline expansion is merely a hint to the compiler that the function is a candidate for inline expansion. Functions that are defined within their own declaration are automatically candidates for inline expansion. This includes class member functions defined within the class declaration.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

An inline function is a sequence of code that the compiler generates in place of the equivalent function call setup, execution, and take down. This is useful for short functions that run so fast that the relative overhead of the function call sequence becomes significant. The downside is that the sequence of code is repeated at each invocation, so the size of the code increases, though the time of the execution decreases.

Most modern compilers take the inline attribute as a hint only, and may or may not actually use the inlined version at a particular call.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Functions make life easier for programmers, essentially allowing the same code to be re-used, over and over again, without having to duplicate that code wherever it is needed. However, function calls are expensive in terms of performance.

For functions that are seldom called, or that perform complex operations, this isn't a major problem. But for simple functions that do very little real work but are called often, performance could suffer. If the time it takes to actually set up a function call and return from that call is greater than the time it takes to actually perform the function itself, then it is clearly better to remove the function call altogether.

That's where inline functions come in. By declaring a function to be inline, you are signalling to the compiler that the function is to be inline expanded, replacing every function call with the actual code of the function. This gives you the best of both worlds -- there's still only one function to maintain and call, and the performance penalty of actually making a function call is removed completely.

However, declaring a function to be inline expanded is no guarantee it actually will be inline expanded. The compiler is free to ignore the request altogether. This is simply because inline expansion increases executable size but also because not every function is a good candidate for inline expansion. The compiler is in a better position to decide which functions will be inline expanded, but you are free to include functions that might otherwise have been ignored by the compiler.

Functions that are defined within their own declarations are automatic candidates for inline expansion, but class member accessors are also prime candidates since most do nothing other than return a constant reference or value.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Inline functions in C++ are functions that the compiler considers for inline duplication at the point of call. This means that a copy of the function is made each time it is called, without generating the overhead of setting up a stack frame and incurring extra time in the call and return sequence. Depending on the size of the function, this can lead to increased speed, at the expense of code bytes. Note that the inline attribute is only a "hint" to the compiler - the compiler can choose to ignore it and generate out of line code.

This answer is:
User Avatar

User Avatar

Wiki User

6y ago

Preprocessor macros are always inline expanded whereas inline functions are only expanded when there is an advantage in doing so. Trivial functions often make ideal candidates as do larger functions that are seldom called, however for other functions, inline expansion increases code size and that can have an adverse effect on performance. With macros the compiler doesn't get the chance to evaluate the benefits (or lack thereof) because it never sees them; they are inline expanded by the preprocessor. But with inline functions, the compiler will choose to override the programmer if inline expansion would result in reduced performance.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the rules for inline functions in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why inline function cannot be static?

Inline functions can be static. However, their usage outside of classes in C++ has been deprecated (a hangover from C). Static member functions are allowed of course, and they can be inline expanded where desired. In C, a static function simply has limited scope within the same translation unit. In C++, unnamed namespaces are the preferred method of achieving the same end.


How does the inline mechanism in C reduce the run-time overheads?

inline functions are compiled very fastly and uses the free memory to boot it as soon as possible


Why functions are not used in c plus plus?

Of course they are used. Both stand-alone and class-member functions are used in C++.


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.


How do you convert from assembly to binary in c plus plus?

Use inline assembly instructions. Then compile your C++ program to produce the machine code.

Related questions

Automatic inline in c plus plus?

The C++ compiler will implicitly (automatically) mark functions for inline expansion whenever you define a function within its own declaration. If functions are declared and defined separately (even in the same file) then they are not implicitly marked for inline expansion. To enable inline expansion for these definitions, you must explicitly mark the definition (not the declaration).


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

yes,we can make function inline


How are the graphs of functions defined by rules like y equals ax plus bx different from those of functions with rules like y equals ax plus c?

The graph of the first form passes through the origin while the second does not - unless c = 0.


Can there be friend functions in c plus plus?

Yes, there can be friend functions in C++.


Inline functions in c plus plus with prgram example?

#include&lt;iostream&gt; using namespace std; inline int max(int a,int b) { return (a&gt;b)?a:b; } int main() { int i1=3,i2=5; cout&lt;&lt;endl&lt;&lt;"Inline function says max is "&lt;&lt;max(i1,i2); return 0; } /* Usually when a function is called, the compiler goes to the particular piece of code and executes it. But in the case of inline functions, the code from the body of the function is effectively pasted at the point of call. inline functions are used when the body of the function is only a line or so.*/


Why inline function cannot be static?

Inline functions can be static. However, their usage outside of classes in C++ has been deprecated (a hangover from C). Static member functions are allowed of course, and they can be inline expanded where desired. In C, a static function simply has limited scope within the same translation unit. In C++, unnamed namespaces are the preferred method of achieving the same end.


Printf and scanf Operators in C and C plus plus?

No, they are functions. Operators are -> or ++or /=


How does the inline mechanism in C reduce the run-time overheads?

inline functions are compiled very fastly and uses the free memory to boot it as soon as possible


Why functions are not used in c plus plus?

Of course they are used. Both stand-alone and class-member functions are used in C++.


What is a method in c plus plus?

In C++, methods are simply class member functions.


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.


How do you convert from assembly to binary in c plus plus?

Use inline assembly instructions. Then compile your C++ program to produce the machine code.