When you declare global variables with the "static" keyword, that use of
static acts like "static" when used with function prototypes/definitions.
It effectively makes that declaration private to the .c file in which the
global is declared.  So if I have test.c and have the following globals:

UInt16 publicInt = 0;
static UInt16 privateInt = 0;

then in test.h I have

extern UInt16 publicInt;
extern UInt16 privateInt;    // This line not necessary...

then publicInt will be accessible from other code modules, whereas
privateInt will not be accessible from anywhere except test.c.  Note that
the .h reference to privateInt is not necessary: it implies privateInt is a
valid parameter outside of test.c, which is incorrect.  If you have it in
the .h file, it will probably lead to confusion, because others will think
privateInt is accessible, but when they write code to try to access
privateInt, the compiler will complain that it doesn't know anything about
privateInt, because it is declared "static".

This is why you see static global vars all over PalmOS, so that those vars
can only be accessed by the .c file in which they are defined, leading to a
more modular system, instead of code in modules x, y and z modifying globals
in modules a, b, and c.  Things get a little difficult to debug when written
like that :)  Other source code that you may have seen probably doesn't use
static globals because the globals needed to be accessed by a number of
different .c files/code modules.  Whether or not that is a good thing is
more of a "religious" topic :)

Cheers,

-DGA

> >Another reason might be for its stack-saving behavior with 
> recursive functions. For example, a recursive routine which 
> declares a local variable static and uses that variable each 
> time it's called, will see that variable remain modified each 
> time the function returns from the recursive call. On the 
> other hand, not declaring that variable static will allow for 
> a unique instance of it during every recursive call, thus the 
> variable will maintain its value each time the function 
> returns from a recursive call. But this has the disadvantage 
> of using additional stack space during each recursion, which 
> could cause stack overflow errors in your application unless 
> the recursion is carefully limited.
> >
> Yes, those are all of the standard reasons to use static local 
> variables, but I was referring to static global variables, 
> which is what 
> I always see in Palm OS code.

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/tech/support/forums/

Reply via email to