Linkers, Loaders & Executables
Where is a non-static local (automatic) variable declared inside a C function placed at run time?
- AIn the .data section
- BOn the stack (or in a register)
- COn the heap
Show answer and explanation
Answer: B. On the stack (or in a register)
A local variable is created in the stack frame when the function is called (optimization may keep it only in a register) and disappears when the function returns. So even when the same function is called many times recursively, each call has its own variables. The executable has no section for locals; the function reserves space by moving the stack pointer at entry.
- A. .data is for globals and similar.
- B. Correct. It is created in the stack frame each time the function is called.
- C. The heap is for memory obtained with malloc and similar.