I have an assignment to create an array of integers that behaves as a Stack. all of my push, pop functions work perfectly fine but im running into a problem when I try to double the size of my array(this happens when I try to push on a full array). Here's the code I have so far...
From header file:
class Stack
{
public:
int size, front, back;
void createStack(int);
void push(int);
void extendSize();
void printElements();
int pop();
int getSize();
bool isEmpty();
bool isFull();
private:
int* stackArray;
int* newArray;
};
- class Stack
- {
- public:
- int size, front, back;
- void createStack(int);
- void push(int);
- void extendSize();
- void printElements();
- int pop();
- int getSize();
- bool isEmpty();
- bool isFull();
- private:
- int* stackArray;
- int* newArray;
- };
The function im trying to implement:
void Stack::extendSize()
{
newArray = stackArray;
stackArray = new int[size*2];
stackArray = newArray;
size *= 2;
}
- void Stack::extendSize()
- {
- newArray = stackArray;
- stackArray = new int[size*2];
- stackArray = newArray;
- size *= 2;
- }
extendSize() does WORK but when I print out the elements in the stack it looks something like this:
Index 0 = 3
Index 1 = 3
Index 2 = 3
Index 3 = 3
Index 4 = 3
Index 5 = 3
Index 6 = 3
Index 7 = 3
Index 8 = 3
Index 9 = 3
Index 10 = 3
Index 11 = -1414812757
Index 12 = -1414812757
Index 13 = -17891602
Index 14 = 0
Index 15 = 0
Index 16 = 1522491621
Index 17 = 469763407
Index 18 = 5001472
Index 19 = 5000384
The elements at the end aren't set to 0 so I eventually get a corruption in the heap. Any advice?