How're static local variables differentiated by compiler or processor at run-time?

Viewed 69

I heard that statics and globals are stored in the same section. And if that's not true, like global variables static locals won't be removed from memory till code is unloaded or program exits.

Consider the following code:

void f1() {
static int i;
...
...
}

void f2() {
static int i;
...
...
}

if both i's are in same section, how will processor differentiate between them? And how does processor load corresponding static locals when a function is called?

This question can be extended to multi-file global-static variables.

1 Answers

Each variable should have its own unique location and SCOPE! The scope is important and every variable has a scope associated with it. That is what keeps int i in f1 separate from int i in f2. So you can see since this is present in normal variables, the same applies to static variables.

Your question slightly confuses me but I think I answered it. If you're asking where the memory address is, there is no way to predict that.

Related