CS253: Software Development with C++

Spring 2021

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 static 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;
}
      0x401b50 exit
      0x4051c8 t1
      0x608118 d1
      0x608120 d2
      0x608140 cout
      0x608258 b1
      0x608268 b2
     0x10832b0 s2[0]
     0x10832e0 s3[0]
0x7ffd4ca66210 s3
0x7ffd4ca66230 s2
0x7ffd4ca66238 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