Dynamic Array

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?

// Container with dynamic storage
#include <iostream>
#include <iomanip>
#include <cassert>

using namespace std;

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;
}

container::container(container &c)
{
data = c.data;
count = c.count;
capacity = c.capacity;
}

container::~container()
{
delete[] data;

}

container& container::operator=(const container &rhs)
{
if (count == rhs.count)
{
for (int x = 0; x < rhs.count; x++)
data[x] = rhs.data[x];
capacity = rhs.capacity;
count = rhs.count;
}

else if (count < rhs.count)
{
delete[] data;
data = new int[count];
for (int x = 0; x < rhs.count; x++)
data[x] = rhs.data[x];
capacity = rhs.capacity;
count = rhs.count;
}

else
{
cout << "Can not perform operation due to lack of memory!";
}

return *this;
}

void container::insert(int x)
{
if (isFull() != true)
{
data[count] = x;
count++;
}
else allocate();
}

void container::remove()
{
if (count >= 0)
{
count--;
}
else cout << "The container is empty; no action is taken!";
}

int container::operator[](int sub)
{
if (sub >= 0)
return *data;
else cout << "Sub out of range!";
}

bool container::isFull()
{
return (capacity == count);
}

bool container::isEmpty()
{
return (capacity-1 == 0);
}

int container::Capacity()
{
return capacity;
}

int container::size()
{
return count;
}

void container::resize(int n)
{
int *temp;
temp = new int[n];
temp = data;
delete[] data;
data = new int[n];
data = temp;
delete[] temp;
}

void container::allocate()
{
int *tmp;

capacity = capacity * 2;
tmp = new int[capacity];
*tmp = *data;
for (int x = 0; x < capacity / 2; x++)
tmp[x] = data[x];
delete[] data;

data = new int[capacity];
for (int x = 0; x < capacity / 2; x++)
data[x] = tmp[x];
delete [] tmp;
}

int main()
{

container c1, c2, c3, c5(c1), c6, c7, c8, c9;

cout << "Before inserting any value into the c1 container" << endl
<< c1;

cout << "\n\nWe now insert 11 to 99 with incremental value of 11 into c1 container." << endl;
for (int i = 0; i < 11; i++)
{
c1.insert(11);
}
cout << c1;
c2 = c1;
cout << "\nc2:\n" << c2;
cout << "\nc5:\n" << c5;

container c4(c2);

cout << "\nc4:\n" << c4;

system("pause");
return 0;
}

One problem is in your copy ctor
1
2
3
4
5
6
container::container(container &c)
{
data = c.data;
count = c.count;
capacity = c.capacity;
}

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.
Topic archived. No new replies allowed.