Inline Functions in C++ | Advantages and Disadvantages

By
Advertisement
The major reason why we are using functions in our program is to save some memory space, which becomes appreciable when a function is likely to be called many times. However, every time a function is called, it takes a lot of extra time in executing a series of instructions for tasks such as jumping to the function, saving registers, pushing arguments into the stack, and returning to the called function. When a function is small, a substantial percentage of execution time may be spent in such overheads.


The solutions to execution time overhead.

  1. The C solution to this problem is use Preprocessor macros. The major drawback with macros is that they are not really functions and therefore, the usual error checking does occur during compilation.
  2. C++ has different solution to this problem. To eliminate cost of calls to small functions, C++ proposes a new feature called inline function. An inline function is a function that is expanded in line when it is invoked. That is compiler replaces the function call with the corresponding function code.

How to Define / Create inline functions in C++?

    There are two ways to define a function inline one is implicit way where a function defined inside a class will automatically become inline if necessary. The other explicit way is use the keyword " inline before any function return type in it's prototype.

(A) Defining inline function within class

It is possible to define short functions completely within a class declaration. When a function is defined inside a class declaration, it is automatically made into an inline function (if possible). It is not necessary (but not an error) to precede its declaration with the inline keyword. 

For example, the preceding program is rewritten here with the definitions of init( ) and show( ) contained within the declaration of myclass .

Example:

#include
using namespace std;
class myclass {
int a, b;
public:
// automatic inline
void init(int i, int j) { a=i; b=j; }
void show() { cout << a << " " << b << "\n"; }
};
int main()
{
myclass x;
x.init(10, 20);
x.show();
return 0;
}

(B) Defining inline function using keyword "inline"

         It is easy to define a function inline. All we need to do is to prefix the keyword inline to the function definition. All inline functions must be defined before they are called.

Example:

#include
using namespace std;

inline int max( int a, int b)       // inline function.
{
   return a>b?a:b;
}

main()
{
cout<
return 0;
}

 Usually, the functions are made inline when they are small enough to be defined in one or two lines.
The compiler may ignore this request if the function definition is too long or too complicated and compile the function as a normal function. Some of the situations where inline expansions may not work are:
  1. For unctions returning values, if a loop, a switch, or a goto exists.
  2. For functions not returning values, of a return statement exists.
  3. If unction contain static variables.
  4. If inline functions are recursive. 

References:
  • OOP with C++, E Balagurusamy
  • OOP with C++, Complete Reference  
 

0 comments:

Post a Comment