Storage Class


The storage class of a C++ variable determines its "lifetime", how long it remains in the computer's memory.

C++ program memory layout

The memory of a C++ program running on Unix consists of the following sections:

  1. Text segment
  2. Static data region
    1. Initialized data segment
    2. Uninitialized data segment
  3. Stack
  4. Free store

The text segment (a.k.a. the code segment) contains the executable instructions of the program. The remaining sections of memory are used to store the program's data. Where a variable is stored depends on its storage class.

C++ variables essentially belong to one of three storage classes: