Heap Corruption Detected
Oct 20, 2013 at 11:18pm UTC
I keep getting an error when i do more than 2 inputs (which is when my resize function gets called) its heap corruption detected after #176 if anyone can spot out what I did wrong that. Ive tried changing the deletes and my sizes but still nothing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#pragma once
using namespace std;
template <class T>
class myStack
{
private :
void resize();
int MAX_SIZE;
int CURRENT_SIZE;
T* stack;
public :
void push(T newItem);
void pop();
T top();
bool isFull();
bool isEmpty();
public :
myStack();
~myStack();
};
template <class T>
myStack<T>::myStack()
{
MAX_SIZE = 1;
CURRENT_SIZE = 0;
stack= new T[MAX_SIZE];
}
template <class T>
myStack<T>::~myStack()
{
delete [] stack;
}
template <class T>
void myStack<T>::push(T newItem)
{
if (isFull())
{
resize();
}
stack[CURRENT_SIZE++]= newItem;
}
template <class T>
T myStack<T>::top()
{
return stack[CURRENT_SIZE-1];
}
template <class T>
void myStack<T>::pop()
{
CURRENT_SIZE--;
if (MAX_SIZE <= 3 * CURRENT_SIZE)
resize();
}
template <class T>
void myStack<T>::resize()
{
T* newstack = new T[max(CURRENT_SIZE*2,1)];
MAX_SIZE=(CURRENT_SIZE*2,1);
for (int i=0; i< CURRENT_SIZE;i++)
{newstack[i] = stack[i];}
delete stack;
stack = newstack;
}
template <class T>
bool myStack<T>::isFull()
{
return (CURRENT_SIZE == MAX_SIZE);
}
template <class T>
bool myStack<T>::isEmpty()
{
return (CURRENT_SIZE == 0);
}
Oct 20, 2013 at 11:25pm UTC
Why are you resizing the array on pop?
Why does resize work on CURRENT_SIZE, rather than MAX_SIZE?
Topic archived. No new replies allowed.