Reputation: 435
I have the following code, where I am allocating memory for three int* in the constructor of the class. And deleting it later, in the destructor of the class. I am having an issue deleting two of the int*, in the destructor (I have put the comments in the code below, where I am having the issue):
#define CAP 3
class SetOfStacks
{
private:
int* a1;
int* a2;
int* a3;
int index = -1;
public:
void push(int data);
SetOfStacks()
{
a1 = new int[CAP];
a2 = new int[CAP];
a3 = new int[CAP];
}
~SetOfStacks()
{
delete [] a1; //This works just fine
delete [] a2; //heap corruption here
delete [] a3; //heap corruption here
}
};
void SetOfStacks::push(int data)
{
if (index >= 3 * CAP)
{
cout << "stack overflow" << endl;
return;
}
if(index>=-1 && index<=1)
{
index++;
a1[index] = data;
}
else if (index >1 && index<=4)
{
index++;
a2[index] = data;
}
else if (index >4 && index<=7)
{
index++;
a3[index] = data;
}
}
int main()
{
SetOfStacks s;
s.push(10);
s.push(20);
s.push(30);;
s.push(40);
s.push(50);
s.push(60);
s.push(70);
s.push(80);
s.push(90);
return 0;
}
I debugged the code many times, but, I am not sure why I am getting HEAP CORRUPTION when I delete[] a2 and when I delete[] a3. Performing delete[] a1 works just fine. I am getting the following error on delete[] a2 and delete[] a3:
Do you know why I am getting this error, and what is causing it (I am confused why deleting a1 works fine but, deleting a2 and a3 is giving this error - though they go almost through the same logic in code)?
Upvotes: 1
Views: 805
Reputation: 2103
index
is out of your array bounds:
if(index>=-1 && index<=1)
{
index++;
a1[index] = data; // index can be 0, 1 or 2 (fine)
}
else if (index >1 && index<=4)
{
index++;
a2[index] = data; // index can be 3, 4 or 5 (all out of range)
// This fixes it:
// a2[index - 3] = data; // index can now only be 0, 1 or 2
}
else if (index >4 && index<=7)
{
index++;
a3[index] = data; // index can be 6, 7 or 8 (all out of range)
// This fixes it:
// a3[index - 6] = data; // index can now only be 0, 1 or 2
}
I would also recommend making the conditions more representative of the actual indices being used so this kind of error is easier to spot:
if(0 <= index && index < 3) {
a1[index++] = data;
} else if (index < 6) {
a2[index++ - 3] = data
} else if (index < 9) {
a3[index++ - 6] = data;
}
Upvotes: 1
Reputation: 39959
All of your arrays have a length of CAP
, which is 3
(judging by your if-statements), but you don't relativize your indices when writing to the arrays.
else if (index >4 && index<=7)
{
index++;
a3[index] = data;
}
In these lines, you could be writing an index of up to 8
to the array.
This is why you are getting the error:
CRT detected that the application wrote to memory after end of heap buffer.
You can fix this by using the modulo operator when writing to the indices using index % CAP
.
For this to work properly with any CAP
, your if-statements should also use >= N * CAP
as boundaries.
else if (index >= 2 * CAP) // no upper check needed in an if-else chain
{
a3[index++ % CAP] = data; // you can also do index++ after
}
Upvotes: 2