CS253: Software Development with C++

Fall 2022

Memory Segments

Show Lecture.MemorySegments as a slide show.

CS253 Memory Segments


Mnemosyne, goddess of memory

Segments

There are several “segments”, or areas of memory, where parts of your program live, in a typical computer architecture:

Text: executable code

Data: initialized non-dynamic non-stack data

BSS: uninitialized static data

Well, I’d like to see you find an image appropriate for BSS!
Brain Salad Surgery

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 [addr, name] : stuff)
       cout << setw(14) << addr << ' ' << name << '\n';
   delete[] s2;
}
      0x401680 exit
      0x404608 t1
      0x607170 d1
      0x607178 d2
      0x607180 cout
      0x607298 b1
      0x6072a8 b2
     0x18c12b0 s2[0]
     0x18c12e0 s3[0]
0x7ffe4d6993b0 s3
0x7ffe4d6993d0 s2
0x7ffe4d6993dc s1

What is in each memory segment?

Summary

TextDataBSSStackHeap
instruction/constinitialized globaluninitialized globallocal vardynamic memory
allocated at compile-timeallocated at run-time
read-onlyread-write
occupies space in a.outno space in a.out