4

I know that a static local variable is in existence for the life of a program. But does a static local variable maintain the same memory address?

Or does the compiler just ensure that it exists and can be accessed within the local scope?

3 Answers 3

4

In C objects don't move around during their lifetime. As long as an object exists, it will have the same address.

Variables with static storage (this includes variables with block scope declared as static) have a lifetime that covers the whole execution of the program, so they have a constant address.

Sign up to request clarification or add additional context in comments.

Comments

3

There's very little difference between local statics and regular globals.

int x = 42; //static lifetime, external name
static int y = 43; //static lifetime, no external name, 
                   //referencable in all scopes here on out
                   //(unless overshadowed)
int main()
{
   static int z = 44; //like y, but only referencable from within this scope
                      //and its nested scopes
   {
       printf("%p\n", (void*)&z));
   }
}

All of these have fixed addresses once the program has been linked and loaded.

Local statics are like globals, except they're only referencable (by their name) from within their scope and its nested subscopes. (You can refer to them from unrelated scopes via pointers.)

Comments

2

Yes, the address offset of every static variable is known at the compile time. When the binary is loaded into the memory, local variables are stored in the .data segment of the program's address space.

In other words, the address of a static variable won't change during the code execution.

8 Comments

.data is platform specific (and static variables often go in .bss).
@melpomene Ah, true! Isn't it also dependant on whether or not the variable is initialized?
Yes, and whether it is const (which might make the compiler place it in .rodata). But all of this is implementation specific.
The actual address in the machine is generally not known at compile time. The compiler may determine an offset within some section of the program, but the sections are often assigned addresses at link time or at execution time. (Especially with address space layout randomization, which is intended to frustrate attackers that would take advantage of known addresses, the address is changed on each execution of the program.)
Local variables may be stored in various places, not just a .data segment. A local automatic variable is most often stored on the stack or in a register if optimization permits (or may even vanish when optimization folds it into other expressions). A static variable might be stored in a constant section if it is constant, a data section if it is not constant but has an initial value, or an uninitialized segment like BSS if it does not have an initial value.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.