Friday, Jan 19th

Last update12:59:40 PM GMT

Throw some light on Inline functions

Write e-mail

Just like printf, inline functions are non-standard C and are provided as compiler extensions, their behaviour being defined by the ANSI standard.

The inline comment is a call to the compiler to copy the code of the (inline) function into the object at every place the function is called. That is, the function is expanded at each point of call. Their advantage comes from avoiding the overhead of calling an actual function. The overheads can include saving registers, setting up stack frames, etc. However, when functions are large, the overhead becomes less important. Inlining tends to increase the size of code, because the function is expanded at each point of the call.


int myfunc(int a)
inline int myfunc(int a)

Inlined functions may not be the fastest, but they are definitely better than macros (which people use normally to write small functions) 

#define myfunc(a)\
{  \

The problem with macros is that the code is literally copied into the location it was called from. So if the user passes a "double" instead of an "int", problems could occur. However, if this scenario happens with an inline function the compiler will complain about incompatible types. Thus the problem would be caught during the debugging.

Inline functions should be used when

1. There is a time critical function.
2. A functions is called often.
3. Its a small function.

Places where inlining is avoided:

1. The function calls itself, that is, is recursive.
2. The function contains loops such as for(;;) or while().
3. The function size is too large.

Share this post

Web Hosting