Umesha MS
Umesha MS

Reputation: 2921

how Inline function code is replaced in calling place?

I want to know how inline function call is replaced by the inline code. i read in some book saying compiler may treat the inline function as normal function.

can any own explain how inline function works.

Upvotes: 2

Views: 4037

Answers (4)

Vishesh
Vishesh

Reputation: 306

By declaring a function inline,then this function is replaced with its definition directly where it is called. It saves the execution time, because generally the normal method calls consists of control transfer to that function and giving memory to that execution differently.

By doing inline, the function gets executed as inline, which removes the control transfer to that function's body.

Generally function is made inline by adding a keyword inline in front of its return type in its declaration.

Upvotes: 2

Ajameerpasha
Ajameerpasha

Reputation: 11

By inline function we can avoid the function overhead and we can save the execution time also.

Example: inline mul (int a, int b)
{
int multi;
multi = a * b;
}

by this we can avoid the function overhead (means storing the pc in stack push & pop operation)

Upvotes: 1

Jim Deville
Jim Deville

Reputation: 10662

inline is a suggestion for the compiler. If the compiler can do it while doing it's other optimizations, it will inline the function, meaning that it will put the function contents "inline" at the callsite, thus avoiding the stack creation, and the call overhead. However, as stated, it is a suggestion and the compiler is not required to do it

Upvotes: 0

Crashworks
Crashworks

Reputation: 41374

From the C++ FAQ:

When the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream (conceptually similar to what happens with a #define macro). This can, depending on a zillion other things, improve performance, because the optimizer can procedurally integrate the called code — optimize the called code into the caller.

There are several ways to designate that a function is inline, some of which involve the inline keyword, others do not. No matter how you designate a function as inline, it is a request that the compiler is allowed to ignore: it might inline-expand some, all, or none of the calls to an inline function. (Don't get discouraged if that seems hopelessly vague. The flexibility of the above is actually a huge advantage: it lets the compiler treat large functions differently from small ones, plus it lets the compiler generate code that is easy to debug if you select the right compiler options.)

In the simplest case, the inline function is dropped into its call site as if you had copy-and-pasted it there. Thus for,

inline int madd( int a, int b, int c ) 
{
   return a * b + c;
}


void foo( int data[3] )
{
   int result = madd( data[0], data[1], data[2] );
   printf("%d\n", result); // note to pedants: this is simpler than a cout stream, so there
}

the compiler could turn it into

void foo( int data[3] )
{
   int result = data[0] * data[1] + data[2] ; // madd is replaced inline
   printf("%d\n", result); 
}

Upvotes: 11

Related Questions