Homework 1: Binding of Memory and the Lifetime of Variables

 

Purpose:

First of all, please go through lecture slide on the concept of binding of memory to variables. This homework assignment requires you to observe and think about the binding of storage to variables mentioned in the slide. The goal is to get a deeper insight into three different ways of binding memory storage to variables (static binding, stack-dynamic binding, and heap-dynamic binding) in C++.

 

Download and extract this zip file to get the C++ source program storage.cpp and the Windows executable storage.exe. Examine the C++ code to understand what is going on in the experiments, run the program to see the result of a few experiments, and in particular pay attention to observe the outputs of memory addresses bound to the variables in different contexts of function calls. Answer the following questions based on the code, the observations you made in the experiments, and your understanding of the related reading in the textbook.

 

Questions:

 

1. In the function testFunc( ), is the variable numberOfTimesCalled bound to a fixed virtual memory storage once for all throughout the entire program execution? If so, when does this binding begin? Could the value of numberOfTimesCalled be retained from one function call to the next function call? Why or why not.

 

2. In the function testFunc( ), is the local variable x bound to a fixed virtual memory storage once for all throughout the entire program execution? If not, when does the binding of the memory storage to the local variable x happen? Could this local variable x be bound to different memory storages at different points of time when running the program? Why or why not? Could the value of x be retained from one function call to the next function call? Why or why not.

 

3. Is the global variable gX bound to a fixed virtual memory storage once for all throughout the entire program execution? If not, when does the binding of the memory storage to the global variable gX happen? Could this global variable gX be bound to different memory storages at different points of time when running the program? Why or why not? Could the value of gX be retained from one function call to the next function call? Why or why not.

 

4. Among the variables declared globally or locally in functions in this program, (i) what variables have static allocation (i.e. they are bound to fixed virtual memory addresses throughout the program execution), (ii) what variables have stack-based allocation (i.e. they are bound to new memory storage allocated when there is a new function call to the function where they are declared), and (iii) what variables are related to heap-based allocation of memory storage?

 

5. What is wrong with the code in Part 3 and what happens when you run it?

 

6. Write a program with an infinite loop that keeps allocating heap memory by calling new int[1000] without ever freeing the memory back. Run the program and observe what happens to the overall system performance.

 

7. Write a program and try to declare a very large global array int Global[size1] outside the main function and a very large local array int Local[size2] inside the main function. How big can size1 be and how big can size2 be? Are they the same on your computer?