CS253: Software Development with C++

Fall 2020

Memory Segments

Show Lecture.MemorySegments as a slide show.

CS253 Memory Segments


Mnemosyne, goddess of memory

Segments

Text: executable code

Data: initialized static data

BSS: uninitialized static data

Stack: local variables

Heap: allocated via new

Example

int b1[3], d1=45;
const int t1 = 299'792'458;

int main() {
   static double b2 = 0.0, d2 = 2.718281828;
   int s1 = 10, *s2 = new int[s1];
   vector<int> s3 = {123,456,789};
   map<const void *, string> stuff = {
      {&s1, "s1"}, {&t1, "t1"},
      {&s2, "s2"}, {&b1, "b1"}, {&d1, "d1"},
      {&s3, "s3"}, {&b2, "b2"}, {&d2, "d2"},
      {&s2[0], "s2[0]"}, {&s3[0], "s3[0]"},
      {&cout, "cout"}, {(void *) exit, "exit"},
   };
   for (auto p : stuff)
       cout << setw(14) << p.first
            << ' ' << p.second << '\n';
   delete[] s2;
}
      0x401b50 exit
      0x4050f8 t1
      0x608118 d1
      0x608120 d2
      0x608140 cout
      0x608258 b1
      0x608268 b2
     0x25e32b0 s2[0]
     0x25e32e0 s3[0]
0x7fff058afdc0 s3
0x7fff058afde0 s2
0x7fff058afde8 s1

Summary

TextDataBSSStackHeap
instruction/constinitialized globaluninitialized globallocal vardynamic memory
static datarun-time data
read-onlyread-write
occupies space in a.outno space in a.out