fiddler:
Interesting and thank for clarifying this.One question tho
When allocation static memory does that automatically become a global variable ?
Kim
No. A global (i.e., defined in the "global" scope) variable and static variable are different.
A global can be accessed anywhere. A static can only be accessed in the function it is defined in. A static variable retains its value across subsequent calls to the function.
The memory for a static is, as the name suggests, static - it is always at the same location, so theoretically it can be accessed globally through the address:
int *superGlobal;
void myFunc()
{
static int localStatic = 4;
superGlobal = &localStatic; // Assign address of localStatic to superGlobal pointer
localStatic++;
}
void myOtherFunc()
{
int myLocal;
myFunc();
myLocal = *superGlobal; // "myLocal" now contains whatever localStatic contains.
}
Not something that is done often - it's more common to either return the value from the function, or return a pointer to the static variable. Such functions, though, are not re-entrant, and are frowned on in today's multithreaded environments. Perfectly fine on the Arduino though