eryer said:
Hi,
i have a question about static function...for example:
file1.c
static void foo(void);
.....
void foo ()
{
...
}
in this example, optimizations due to static keyword at compiler time
are performed or not? Or is more correct do something like:
file1A.c
static void foo(void);
.....
static void foo ()
{
...
}
IMO, it is better to have both declarations the same, as in the latter case.
that said, static defines semantics, not optimizations.
static on a function will essentially just hide it from other compilation
units, but typically little more than this.
this is the same semantics as for global variables.
any optimizations done, or not done, due to the use of static are compiler
dependent.
in most cases, there is unlikely to be much of any real difference though...
granted then, if you mean putting a static in a header, vs a non-static
function in another compilation unit, this does effect something: it may
allow the compiler to inline the function (the inline keyword may also serve
as a hint for it to do so as well, although not all compilers accept the
'inline' keyword).
however, in this case, the optimization would be due to the visability of
the function (it being in the same compilation unit, hence the compiler can
see its sourse), and not due to 'static', which in this case would serve
mostly to "hide" the function from other compilation units (such that you
can put the function in a header without risking the linker making a big
fuss and possibly refusing to link the program).
I have observed though that linkers may also silently "merge" all these
duplicate static functions (from headers), such that they don't take up lots
of extra space in the compiled output.
a similar observation is that the compiler may also omit the functions if
they are never called.
but, then again, all this is still due to the semantics, of the modifier,
rather than a result of the modifier itself.
for example, it is very well possible that a compiler will inline a function
even though it does not use any extra keywords, ...
and, it is also very well possible that a compiler will be stupid and not do
anything differently than simply obey the semantics of the keyword (IOW:
avoid a link-time error due to multiple-definition).