CS 222/420 Winter 1999, Instructor: Jeffrey Horn
HOMEWORK 5: DESTRUCTORS
and MEMORY MANAGEMENT
| Handed out/Assigned: | Friday, April 2, 1999 |
| Due: | Friday, April 9, 1999 |
STEPS: For both parts below, fix the code by
adding destructors, "delete" commands, etc., so that
there are no more memory leaks! Both problems
start with leaks. Fix them. Your mains should then run forever.
COMMENT YOUR DESTRUCTORS!
PART 1: (click here to go straight to text file of code)
#include<iostream.h>const int ARRAYSIZE = 100000;
class Individual
{
public:int identification;
int age;
float weight;
float *data[ARRAYSIZE];Individual(int ID) // Constructor
{identification = ID;
age = 10 * ID;
weight = 99;for (int j = 0; j< ARRAYSIZE; j++)
{
data[j] = new float;
*data[j] = j / 3.14;
};
}~Individual() { } // Destructor
// ADD CODE HERE.};
void main()
{
Individual *me;
int count = 0;while(1)
{me = new Individual(count);
cout << count++ << "\n" ;
}
}
PART
2: (click here to go straight to text file of code)
#include<iostream.h>const int ARRAYSIZE = 100000;
const int ARRAYSIZE2 = 10000;class Personal_History
{
public:int *transactions[ARRAYSIZE];
float past_acct_balances[ARRAYSIZE];Personal_History() // Constructor
{
for (int i = 0; i<ARRAYSIZE; i++)
{
transactions[i] = new int;
*transactions[i] = i*55;past_acct_balances[i] = i*10.89;
}
}~Personal_History() { } // Destructor.
// ADD CODE HERE!};
class Individual
{
public:
int identification;
int age;
float weight;
float *data[ARRAYSIZE2];
Personal_History thePast;Individual(int ID) // Constructor
{
identification = ID;
age = 10 * ID;
weight = 99;for (int j = 0; j< ARRAYSIZE; j++)
{
data[j] = new float;
*data[j] = j / 3.14;
};
}~Individual() { } // Destructor
// ADD CODE HERE!};