random crash in push method of stack

As an exorcise I'm trying to implement a Stack from scratch.
When testing the class the push method always crashes the process and rarely at the same point twice in a row. Sometimes it will run for several iterations of pushing a value onto the stack, others it won't make it passed the first.

Class definition:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template< class T>
    class Stack {
        public:
            bool isEmpty();
            size_t size();
            T pop();
            void push(T item);
            Stack();
            Stack(const Stack<T>& cpy);
            ~Stack();
            T peek();
        private:
            T* stack;
            size_t top;            
    };

constructor:
1
2
3
4
template< class T > 
    Stack<T>::Stack() {
        top = 0;
    }

Detor code:
1
2
3
4
5
template< class T >
    Stack<T>::~Stack() {
       delete[] stack;
       stack = NULL;
    }

push method:
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
template< class T >
    void Stack<T>::push(T item){
        if(top == 0) {
            stack = new T[1];
            *stack = item;
            top++;
            return;
        }
        T* tmp = new T[top];
        if(tmp) {
            for(int i = 0; i < top ; i++) {
                tmp[i] = stack[i];
            }
            top++;
            delete[] stack;
            stack = new T[top];
            for(int i = 0; i <= top; i++) {
                stack[i] = tmp[i];
            }
            delete[] tmp;
            tmp = NULL;
            stack[top-1] = item;
            return;
        } else {
            return;
        }
    }

test code
1
2
3
4
5
6
7
8
9
#include <iostream>
int main(void) {
    Stack<int> stack_o_intergers; 
    for(int i = 1; i <= 20; i++) {
        stack_o_intergers.push(i);
        std::cout << (i) << " has been pushed to stack" << std::endl;
    }
    return 0;
}


it also seems to crash at different points in the function.

edit: Fixed some errors I introduced while testing this issue.

Edit 2: Also, it seems to only crash when calling delete[] or new[] on either of the pointers.

edit 3: well crap, it even crashes if I just create an object of the Stack and then return 0. Fixing another error and adding the detor code.
Last edited on
1
2
3
for(int i = 0; i <= top; i++) {
	stack[i] = tmp[i];
}

When this code runs stack has pos-1 elements and tmp has pos-2 elements so you will be accessing elements out of bounds.

If you don't want to handle when pos == 0 as a special case you should initialize stack in the constructor.
Last edited on
Wow. Thanks for pointing that out!

Yea, it's usually something you just happen to overlook :\
¿Why are you making 2 copies?
By the way, new will throw an exception if it fails
Initially I was just deleting stack and assigning tmp to stack. But, at some point I thought I might be corrupting the heap and went on a wild goose chase introducing more bugs with each test :P

Also when I tried to catch bad_alloc during testing it never actually threw an exception.

Lol, did I mention this exercise was because I haven't really used C/C++ in a little over a year? Kinda rusty :P
Topic archived. No new replies allowed.