This is the code im working on for my cs3420 class. I really don't understand dynamic arrays. I cant figure out why I am getting a HEAP CORRUPTION DETECTED error after my deconstructor runs. Any ideas?
class container {
friend ostream& operator<<(ostream& out, container &);
// Postcondition: displays # of values stored in the container, storage capacity of the contianer, and stored values in the container
// in the following format: Array size = 3, capacity = 4, contents = 11, 22, 33 (see below sample program output
public:
container();
// Postcondition: set dynamic storage capacity to 1 and count to -1 where (count + 1) represents the actual values stored
// in the container. Notice that data member count is used as the subscript to access elements (actual values) stored
// in the dynamic array; thus (count + 1) represents the total # of values that are currently stored in the array
container(int n);
// Postcondition: set dynamic storage (data array) capacity to n and count to -1
container(container &);
// Programmer-supplied copy constructor is necessary to avoid memory leak and other side effect
// Postcondition: a new container class object is created which is the same as the one passed to the function
~container();
// Programmer-supplied destructor is necessary to avoid memory leak
// Postcondition: all dynamic memory locations have been returned back to the heap whenever a container object goes out of scope
container& operator=(const container &rhs);
// Programmer-supplied overloaded assignment is necessary to avoid memory leak and other side effect
// Postconditoin: the container object rhs is assigned to the calling object
void insert(int);
// Postcondition: if the container is not full, the value passed to the function is stored in
// the first available element of the dynamic array. Otherwise the function calls the private
// "allocate" member function requesting a new set of dynamic memory with twice the previous storage capacity
// the insert function then increments count by 1 and insert the value into the new and larger array.
void remove();
// Precondition: the data array must not be empty; i.e., count must be greater than or equal to 0.
// Postcondition: if the container is not empty, then remove the most recently stored value ifrom the container and
// decrement count by 1; otherwise, display the message "The container is empty; no action is taken!"
int operator[](int sub);
// Precondition: value passed to the function must be a positive integer including 0
// Postcondition: the value of stored in data[sub] is returned; if sub is out of range, display a message and terminate the program .
bool isFull();
// Postcondition: return true if the container is full; return false otherwise
bool isEmpty();
// Postcondition: return true if the container is empty; return false otherwise
int Capacity();
// Notice uppercase 'C' to avoid conflict with data member named "capacity"
// Postcondition: returns the current storage capacity of the container
int size();
// Postcondition: returns the # of elements (# of objects) currently stored in the container
void resize(int n);
// Postcondition: container (i.e., the dynamic array) is resized to n; contents of existing container have been copied to the new array;
// old array is deleted to avoid memory leak.
private:
void allocate();
// Postcondition: 1) the capacity of the container has been doubled, 2) existing values in the existing array have been copied to
// the new and larger dynamic array, 3) memory of the old array has been deleted (returned to "heap").
int *data;
int capacity; // indicates the storage capcity of the container, i.e., the size of the dynamic array
int count; // used as a subscript to index into the array; size = count + 1
};
ostream& operator<<(ostream& out, container &c)
{
out << "Container capacity = " << c.capacity
<< " Size or the actual # of values stored in container ="
<< c.count;
if (c.isEmpty() == true)
{
out << "\nContents of the container = empty!\n";
return out;
}
else if (c.isFull() == true)
{
cout << "\nValues in the stored container are: ";
for (int x = 0; x < c.capacity; x++)
out << c.data[x] << " ";
out << "\nContents of the container = full!\n";
return out;
}
else
{
out << (c.count + 1);
for (int x = 0; x < c.capacity; x++)
out << c.data[x] << " ";
return out;
}
}
container::container()
{
data = new int[1];
assert(data != NULL);
count = 0;
capacity = 1;
}
container::container(int n)
{
data = new int[n];
assert(data != NULL);
count = 0;
capacity = n;
}
After container c4(c2); c4.data == c2.data
Both dtors will delete the same memory which causes the problem.
To solve the program do some research about "deep copy"
Also your allocate and resize function are buggy, but one thing at a time.
Didn't your course teach you about dynamic memory management?
Also you should write your code in small chunks and test after each step.