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

 

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++ (not under Linux).  It shows, for the memmodel.cpp program, the state of the memory model at the point where execution first 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 two analogous diagrams for your execution.  These two will record the state of memory at the two points 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

 

Layout of memory not under Linux where execution first outputs Leave ‘sum’

 

Name

Address (in increasing order)

Size

Contents

Area (Global, Stack, Heap)

Function name (for stack frame)

 

0

 

 

 

 

 

...

 

 

 

 

sh

6019b0

2

-1

Globals

(none)

j

6019b4

4

13

Globals

(none)

p

6019b8

8

6019b4

Globals

(none)

D

6019c0

8

1002

Globals

(none)

L

6019c8

8

1003

Globals

(none)

 

...

 

 

 

 

b

7fff43664db8

8

3.3

Stack

sum

a

7fff43664dc0

8

1.1

Stack

sum

c

7fff43664dd0

8

4.4

Stack

sum

f

7fff43664e20

8

3.3

Stack

main