Assignment 2a.  Diagramming the Memory Model for C++

V1.1 

 

A C++ (or C) program has a memory model with three areas for data: (1) an area for globals, (2) the stack and (3) the heap

 

Variables declared outside of functions and types are called globals.  These are stored in the globals area.

 

Each call to a function allocates space to store variables declared in (and hence local to) the function.  In particular, when a function is called, a stack frame to hold its local variables is pushed on the top of the stack.  The frame is popped off the stack when the function returns.

 

There is a heap from which space is allocated dynamically, via the C++ “new” operator .

 

Below is a diagram of memory, as it was observed during an execution of an implementation of C++.  It shows, for the memmodel.cpp program, the state of the memory model at the first point where execution outputs the message Leave ‘sum’.   The addresses in these diagrams may be given in hexadecimal (base 16).

 

You are to run this program on a Waterloo student computer under Linux and g++.  You are to create an analogous diagram for your execution.  Your diagram will record the state of memory at the second point where the memmodel.cpp program outputs the message Leave ‘sum’.  In your diagram list the addresses (as output by memmodel.cpp on the Waterloo student  Linux computer), as well as the variables’ names, sizes, contents, area (globals, stack or heap) and containing stack frame (if any).  The entries in your table should be increasing order of the addresses of the variables.

Submit this assignment in PDF format in a file named mem.pdf

 

Here is the layout of memory when execution first outputs Leave ‘sum’.  You are to give the layout when the execution again outputs Leave ‘sum’.

 

Name

Address (in increasing order)

Size

Contents

Area (Global, Stack, Heap)

Function name (for stack frame – or none)

 

0

 

 

 

 

 

...

 

 

 

 

si

602088

2

11

Globals

(none)

j

60208c

4

130

Globals

(none)

p

602090

8

60208c

Globals

(none)

D2

602098

8

1332

Globals

(none)

L3

6020a0

8

1443

Globals

(none)

 

...

 

 

 

 

b

7fff37be00b0

8

3.3 1.3

Stack

sum

a

7fff37be00b8

8

2.2 1.3

Stack

sum

c

7fff37be00c8

8

5.5 2.6

Stack

sum