>
> Hi,
>
> there is no difference between a file and a function static.
>
> I'm often using static because if complete initialisation is not done,
> the results can be random results and compiler and environment
> dependent bugs.
>
> Regards,
>
> Rolf
>


That's not quite true - there are *some* differences between file static and
function static, though not as much as many people think.  People think they
are completely different, as they are looking at normal ("auto") function
variables vs. static function variables, and normal (global) file variables
and static file variables.

The key points about making a variable "static" is that it will have a
single linker-allocated absolute memory address (like a global variable),
but with a limited scope (like a local variable).  This means that the data
is initialised (to zero by default, or a specified value) on program
startup, and retains its value throughout the program (except when
explicitly changed in code, obviously!).  It also means that the compiler
knows exactly when and where the data is accessed when it compiles the
file/function containing the variable, and can make use of that information
for optomisation.

Declaring file data (and functions) as static is a very good idea - in my
code, every file-scope variable or function is either an exported global
object (with a corresponding "extern" in the corresponding hearder file) or
declared static, with a gcc warning otherwise.

Declaring function data as static is very seldom necessary.  If you are
using a horrible processor (with few registers and limited stack-based
addressing modes) with a poor compiler, it makes sense to make function data
"static", as absolute addressing modes are faster than stack-based modes on
such processors.  On the msp430, that is not the case - it has plenty of
registers, and fast stack access.  So ordinary local variables are much
faster and use smaller code, and are the correct form to use for normal
data.  There are only two reasons to use function static: when you want
global data that is only accessable from within the one function (a normal
file-static global will do the same job, and often be more maintainable), or
on occasion when you have a large local variable that you don't want to put
on the stack (perhaps in a multi-threading system when you want to keep
stack size low).

Using "static" as a way of initialising local variables is very bad
programming.  Orginary local variables can equally well be initialised in
the same way, without the large penalty in code size and speed (and
readability).  Abusing "static" is not a substitute for writing correct
code - especially when the "-Wunitialized" warning will let gcc tell you
about such problems.  If you don't know about gcc's warnings, then it really
is time to read the manual!  As a starter, most users should have:
    -Wall -W (this is -Wextra for gcc3.4 onwards)
Other warnings for bad code, with little conflict with good code, are:
    -Wpointer-arith -Wcast-qual -Wcast-align -Wstrict-prototypes
    -Wunreachable-code -Wredundant-decls -Wpadded -Wnested-externs

To force good use of "static" and "extern", use:
    -Wmissing-prototypes -Wmissing-declarations


The following two functions illustrate "static" - they are virtually
identical in their semantics and implementation:

//------------------
int counter(void) {
    static int foo;
    foo++;
    return foo;
}
//------------------

//------------------
static int counter_foo;
int counter(void) {
    counter_foo++;
    return counter_foo;
}
//------------------

Hope that helps,

David



Reply via email to