Linkers, Loaders & Executables

Where is a non-static local (automatic) variable declared inside a C function placed at run time?

  1. AIn the .data section
  2. BOn the stack (or in a register)
  3. 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.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. Memory allocated with C's malloc generally comes from which area of a process?
  2. Which lists the typical memory layout of a process on Linux and similar systems from low to high addresses?
  3. What is the advantage of laying out the heap and stack so they grow in opposite directions with a large gap between them?
  4. Which describes static linking?
  5. What is an advantage of dynamic linking (shared libraries)?