inline functions

J

junky_fellow

What is an inline function ?
When should we declare a particular function as inline ?
If a function is declared to be inline, does that mean
that the entire code for that function is subtituted there
just like macros ? Then why not use macros ?
 
J

Jaspreet

What is an inline function ?
When should we declare a particular function as inline ?
If a function is declared to be inline, does that mean
that the entire code for that function is subtituted there
just like macros ? Then why not use macros ?

Usually when we call a function, the control of the program jumps from
the aclled position to the function that is being called and once the
called function is over control switches back to the initial position
from the jump was made initially. This is slightly slow instead of the
code being placed there so that the control does not jump from one
place to another.

Soo, if you want the code to be replaced instead of the function call
you can make your function as inline. However, the speed will be faster
but your size of the object file will increase since every call of the
function will be replaced by its code.

Macros are somewhat different since they are pre-processor statements.
Soo, if you are trying to debug a prgram yuo will not find symbols if
you have used macros.

inline is again a request to the compiler which it may ignore. Usually
your program should not be large or containing loops for it to be made
an inline.

Always remember to put an "inline" keyword only at the time of decl. of
the function and not at function definition.
 
J

Jens.Toerring

What is an inline function ?

A function declared with the function specifier "inline". It's
new in the C99 standard (but a lot of compilers allowed it as
an extension already prior to that).
When should we declare a particular function as inline ?

When you have a short function where the overhead of calling
the function is too large, i.e. if there's more time spend in
calling the function and returning from it than in what the
function actually does. Potential candidates could be e.g.

inline int_abs( int x ) {
return x >= 0 ? x : -x;
}
If a function is declared to be inline, does that mean
that the entire code for that function is subtituted there

Not necessarily. The only thing the standard says is that:
"Making a function an inline function suggests that calls to
the function be as fast as possible.". Please note the word
"suggests", i.e. the compiler is free to do nothing. So what
the compiler exactly does with inlined functions is left to
the discretion of the implementor. One way to do it is, of
course, to put the code for the function everywhere where
it's called.
just like macros? Then why not use macros ?

Functions and macros are completely different beasts. A
macro is nothing else than a text replacement that happens
at a very early stage of the compilation. Therefore macros
can lead to very strange results unless used very carefully.
Consider e.g.

#define ABS( x ) ( ( x ) >= 0 ? ( x ) : ( -x ) )

This looks quite nice and simple, doesn't it? But try to call
it like this

int a = -1;
int b = ABS( a++ );

and think about what's going to be the result. (Hint: you will end
up with 'b' set to 0 and 'a' being 1. You would expect it to be the
other way round, wouldn't you? To figure out why do the text replace-
ment for the macro by hand and see what's the resulting code.) That's
something that won't happen with a function (inlined or not), since
function arguments are evaluated before passed to the function.

Regards, Jens
 
M

Malcolm

Consider e.g.

#define ABS( x ) ( ( x ) >= 0 ? ( x ) : ( -x ) )

This looks quite nice and simple, doesn't it? But try to call
it like this

int a = -1;
int b = ABS( a++ );

and think about what's going to be the result.

Also try to call ABS(x + 2)

this will resolve to -x + 2

because the parentheses have been misplaced. That's one of the problems with
macros, errors are not easy to catch.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top