At 10:30 PM +0000 2001/10/23, Trevor Harmon wrote:
>For examples of what I'm talking about, take a look at any of the SDK examples. I 
>picked a few at random just now--ToDo.c, SubHunt.c, and SampleCalc.c--and each is 
>riddled with static variables and functions. Another example: The Hello World example 
>at the Palm Programmer's FAQ (http://tangentsoft.net/palmfaq/) has some functions 
>marked static, others not, and I can't figure out why.

Regarding static functions... you'll see a lot of this because it's good to hide the 
existence of all functions that you don't explicitly wish to be exposed. Another way 
of saying this is... that you want to mark all functions *private* except for those 
that you explicitly want to mark as *public* and therefore be exposed outside the 
source files in which they reside.


>It doesn't really matter. Just let me get this straight: If I don't care whether my 
>functions are externally accessible, and as long as I access my global variables 
>during launch codes that allow it, then static is totally unnecessary, right?

No, that's not correct; it does matter.

There are several reasons why you might choose to make a local variable static. 
Usually it's because a function requires some sort of "context" between calls to it in 
your application, but by using static you avoid having to explicitly declare and 
reference a global variable in some header file somewhere.

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.

Also be aware that accessing local variables declared as static from launch codes that 
do not support globals will fail. You'll either corrupt memory and/or cause a bus 
error, depending on what type of static variable is being accessed and how.

Hope that info helps!

Best Regards,

Jim Schram
Palm Incorporated
Partner Engineering


-- 
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