When a compiler inlines a function, it takes the function body and places it directly in the code for the calling function. This eliminates the overhead of a procedure call, which can be a big gain for tiny functions that are called frequently. It can also help with big functions that are called frequently, but only from one or two places in the code. For large functions that are called from many places, it is not such a good idea, as it tends to make the code much larger which can reduce the efficiency. It is also completely impossible with recursion functions.
Under GCC, one requests that a function be inlined by preceeding the
function definition with the keyword inline
. For example:
inline int min(int x, int y) { return (x < y) ? x : y; }
GCC does not actually perform inlining unless you are doing an
optimising compile e.g. with -O2
.
You may have noticed that inlined functions are doing a very similar job to macros. So what are the differences? Here are a few:
Generally it is better to use inline functions than macros where possible, largely due to the first point above.
Last updated Sun Nov 28 22:37:23.0000000000 2004. Copyright Bruce Merry (bmerry '@' gmail dot. com).