I'm a complete noob with dynamically allocated memory. Will this have a memory leak or any other memory problem?
#include <iostream.h>
template <class T> class stack
{
struct node
{
T value;
node* next;
};
public:
stack()
{
size = 0;
}
~stack()
{
while(size > 0)
{
node *n = top->next;
delete top;
top = n;
size--;
}
}
void push(T value)
{
node *n = new node;
n->value = value;
if(size == 0)
{
top = n;
}
else
{
n->next = top;
top = n;
}
size++;
}
T pop()
{
if(size<1)
{
std::cerr<<"Stack underflow"<<endl;
exit(0);
}
else
{
node* n = top;
int val = n->value;
top = n->next;
delete n;
size--;
return val;
}
}
int getSize()
{
return size;
}
private:
int size;
node *top;
};