Linkers, Loaders & Executables

Of the global variables above, which are generally placed in the .bss section?

int a = 5;
int b;
static int c = 0;
const int d = 7;
  1. Aa and b
  2. Bb and c
  3. Cc and d
Show answer and explanation

Answer: B. b and c

a has a non-zero initial value, so it goes in .data; b has no initializer and starts at zero, so it goes in .bss; c is initialized to 0, so there is no need to store a zero in the file and it also goes in .bss. d is a const constant, so it goes in read-only .rodata. static decides visibility from other files; the section is determined by the initial value and const.

  • A. a has the initial value 5, so it is in .data.
  • B. Correct. One has no initializer and one is zero.
  • C. d is a constant, so it is in .rodata.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. Where is a non-static local (automatic) variable declared inside a C function placed at run time?
  2. Memory allocated with C's malloc generally comes from which area of a process?
  3. Which lists the typical memory layout of a process on Linux and similar systems from low to high addresses?
  4. What is the advantage of laying out the heap and stack so they grow in opposite directions with a large gap between them?
  5. Which describes static linking?