Hi
I'm making a template stack which uses stores its data by reference. The weird thing is that it works fine in debug mode, but in release mode, my data seems to get corrupted (or something)
Here's the push code:
template <class T>
void Stack<T>::Push(const T &data)
{
Node<T> *pNode = new Node<T>(data);
pNode->next = top;
top = pNode;
size++;
}
...and the code for pop:
template <class T>
const T& mehs::Stack<T>::Pop()
{
const T &data = top->data;
Node<T> *pNode = top;
top = top->next;
delete pNode;
size--;
return data;
}
...and the code for the Node class
template <class T>
class Node
{
public:
Node(const T &d) : data(d), next(0) {}
virtual ~Node() {}
const T &data
Node *next;
};
When I push 1, 2, 3 on the stack, like this...
Stack<int> s;
s.push(1);
s.push(2);
s.push(3);
in release mode and then pop it, the output is wrong, usually 3,3,3419312.
But, when I push variables, like this...
int a=1, b=2, c=3;
Stack<int> s;
s.push(a);
s.push(b);
s.push(c);
it works fine (even in release mode).
Please help! I can't find what I'm doing wrong.
(I have to make a stack for my C++ course in school, so no use-the-std-stack-flame please)
Regards Mats
[edited by - Matsen on September 16, 2002 2:19:23 AM]
[edited by - Matsen on September 16, 2002 2:20:15 AM]
[edited by - Matsen on September 16, 2002 2:23:35 AM]