C inline functions

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

C inline functions

Postby Crispy » Thu Oct 03, 2013 11:56 am

Hi all,

I am hoping to use inline functions in a piece of C code that I have, but I am a bit confused about their proper use and was hoping someone would be able to help me. As an example, assume that I have a header file foo.h containing the prototype of the function int func_foo() and a source file foo.c containing the declaration of the function func_foo(), e.g.

foo.h
Code: Select all
int func_foo( void );


foo.c
Code: Select all
#include "foo.h"

int func_foo( void )
{
  ...
}


Then I have another file, bar.c, which includes foo.h so it can use the function func_foo(), e.g.

bar.c
Code: Select all
#include "bar.h"
#include "foo.h"

int func_bar( void )
{
  ...
 
  x = func_foo();

  ...
}


Assuming that func_foo() is a good candidate for inlining, I am a bit confused as to the proper way to define the inline function func_foo. Should I place the inline keyword in the function prototype in the header file, foo.h, or in the function declaration in foo.c or both? Also, i've had a look around and some people suggest using the extern keyword in the header file as well as the inline keyword?

Also, if we assume that func_bar() is also suitable for inlining and is called often, is it better for me to inline both func_foo() and func_bar() or just func_bar or func_foo by themselves?

Finally, if we consider an additional file, foobar.c which contains the function func_foobar, e.g.

foobar.c
Code: Select all
#include "foobar.h"

int func_foobar( int (*func)( void ) )
{
  ...

  x = (*func)();

  ...
}


And we assume that (*func) points to an inline function, will the inlining optimisation still hold within the function func_foobar? Or will it simply be treated as a normal function?

Many thanks in advance,

Chris
Crispy
 
Posts: 57
Joined: Mon May 31, 2010 6:35 pm

Return to Programming

Who is online

Users browsing this forum: Google [Bot] and 0 guests