Jared said:
I think this has already been covered, but I'll reiterate. It is most
useful to think of the difference from the linker's point of view.
A static function is one in which only one instance is created. No
matter how you access it functions API, it is always the same code at
the same program counter location which will be executed.
No. A static function has one single "program counter location" in the
translation unit where it's defined. If you want to use that function
in more than one translation unit, each of them will need its own
definition, since the linker won't see any static functions at all.
And if the compiler has the code for a function, it may -- even without
the inline specifier -- choose to inline this function.
An inline function is not really a function at all; it only looks that
way in your source code. The function call is removed, and a copy of
the body of the inline function inserted directly at that point.
Depends on what you mean by "An inline function". If you mean a function
defined as 'inline', then this may not be the case. If you instead mean
a function that is actually inlined, you're right. The inline keyword
is only a hint to the compiler that you'd like this funciton to be
inlined, but the compiler is free to inline functions you didn't
declare as inline or to not inline functions you did. Of course it will
need the code of that function to be able to inline it, so this code
must be know in the translation unit.
It's a little bit like unrolling a loop. If your inlined function is
called all over the place, a new copy of that function is created for
each call.
You don't get anything for free. The speed savings come at a cost of
larger binaries unless the function is only referenced once.
There may even be no speed savings at all. Inlining can easily lead to
slower execution. Same for loop unrolling. Because of the increased
code size, you will get more cache misses in the CPU's code cache, and
ineffective cache use can have a dramatic impact on execution speed.