Linkers, Loaders & Executables

What is the advantage of laying out the heap and stack so they grow in opposite directions with a large gap between them?

  1. AThe gap can be shared flexibly even when it is unknown in advance how much each will use
  2. BBoth can use the same addresses at once, doubling memory
  3. CThe stack can never overflow
Show answer and explanation

Answer: A. The gap can be shared flexibly even when it is unknown in advance how much each will use

Because the heap grows up and the stack grows down, the gap between them serves a program that uses lots of heap as well as one that uses lots of stack. Giving each a fixed size could let one overflow while the other has room to spare. The stack usually has a set limit, though, and recursion that is too deep exceeds it and causes a stack overflow.

  • A. Correct. It makes good use of the virtual address space.
  • B. They cannot use the same addresses at once.
  • C. Deep recursion and the like can still overflow the stack.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. Which describes static linking?
  2. What is an advantage of dynamic linking (shared libraries)?
  3. A dynamically linked program copied to another computer fails to start. What is the most likely cause?
  4. When linking with a static library (.a file), what does the linker put into the executable?
  5. With the GNU linker, writing a static library before the object file that uses it, as in "gcc -lmylib main.o", can cause undefined-reference errors. Why?