Function inlining in C (not C99)

N

Nikos Chantziaras

What's the best way to allow the compiler to inline a function in C? (Not
C99, which already supports the 'inline' keyword.)

The way I do it, is to define the functions in question as 'static' in a
header file:

static void foo()
{
/* ... */
}

Almost every compiler these days will inline the function when compiling
with optimizations turned on (GCC does, I think). Anyway, can something go
wrong with this approach? And if yes, is there a better way to achieve
this?
 
M

Mark Mynsted

Nikos> What's the best way to allow the compiler to inline a function in C? (Not
Nikos> C99, which already supports the 'inline' keyword.)

The best way would be found by reading the documentation that
accompanies your compiler. It may be something a simple as
-finline-functions or not. I do not believe there is a portable
answer to your question.

Nikos> The way I do it, is to define the functions in question as 'static' in a
Nikos> header file:

This MAY make your functions have more potential to be inlined by your
compiler, but it may not. It depends on your compiler. The static
keyword for functions has other ramifications. To net this out, make
functions static where they should/can be static, but not because they
will be more likely to be inlined.

Nikos> Almost every compiler these days will inline the function when compiling
Nikos> with optimizations turned on (GCC does, I think). Anyway, can something go
Nikos> wrong with this approach?

Yes. If there were no trade-offs why would it be an option? There
are other ways to improve the performance of your code. Maybe you
need a better algorithm vs. inlining... There are many other ways to
improve performance where it is needed.



--
-MM
I rarely read email from this address /"\
because of spam. \ / ASCII Ribbon Campaign
I MAY see it if you put #NOTSPAM# X Against HTML Mail
in the subject line. / \
 
D

Dan Pop

In said:
What's the best way to allow the compiler to inline a function in C? (Not
C99, which already supports the 'inline' keyword.)

The way I do it, is to define the functions in question as 'static' in a
header file:

static void foo()
{
/* ... */
}

Almost every compiler these days will inline the function when compiling
with optimizations turned on (GCC does, I think). Anyway, can something go
wrong with this approach? And if yes, is there a better way to achieve
this?

The only reliable way of achieving what you want is to use macros instead
of functions (where possible).

Dan
 
M

Malcolm

Nikos Chantziaras said:
What's the best way to allow the compiler to inline a function in C?
(Not C99, which already supports the 'inline' keyword.)
Compile under C++.
 
M

Malcolm

[ inline by declaring static in header ]
On downside is that if you port to a compiler that doesn't do the
inlining, you end up with a copy of foo()'s code in every .c module that
includes that .h. Multiply by the number of functions you've done this > to.
Since you expect the function to be inlined it can't be too big.
Anyway, code size is seldom much of a problem these days.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top