i get a heap corruption error when i run the following code...
bool Deque::insertBack(int input)
{
cout << "Back is " << back << endl;
if(isFull() == 0)
{
Array[back] = input;
back = (back - 1) % size;
return true;
}else{
cout << "Deque is Full " << input << " not inserted" << endl;
return false;
}
}
i cant seem to figure it out and ive been playing around with it for a good while, also my insertFront function works fine.
This is the full class...
Deque::Deque(int iSize)
{
size = iSize;
Array = new int[size]; // initialise array size
front = 0;
back = 0;
}
Deque::~Deque()
{
delete [] Array;
}
bool Deque::isFull()
{
bool full = false;
cout << back << " " << ((front + 1) % size) << endl;
if(back < (front + 1) % size)
{
// deque is full
full = true;
}
else
{
full = false;
}
return full;
}
bool Deque::insertFront(int input)
{
cout << "Front is " << front << endl;
if(isFull() == 0)
{
Array[front] = input;
front = (front + 1) % size;
return true;
}else{
cout << "Deque is Full " << input << " not inserted" << endl;
return false;
}
}
bool Deque::insertBack(int input)
{
cout << "Back is " << back << endl;
if(isFull() == 0)
{
Array[back] = input;
back = (back - 1) % size;
return true;
}else{
cout << "Deque is Full " << input << " not inserted" << endl;
return false;
}
}
void Deque::displayDeque()
{
for(int i=back;i < front;i++)
{
cout << Array[i] << " is at postion " << i << endl;
}
}
This is likely the problem causing your heap corruption:
In C++, taking the modulus of a negative number results in a negative number. So you can get the same effect using:
Also, your
displayQueue()
function does not take into account the modulus fori
.