I am starting to learn about how to use stacks. However I am having a problem with popping off the stack. I was wondering if anyone could explain to me why my code will push onto the stack just fine, but I can't output when I pop. The error I get is memory fault (core dumped). It seems like if I push one piece of data onto the stack, then obviously the stack should not be empty. So I guess my question is why can I not pop the item off directly after pushing it on? I am using Unix through a Putty console if it makes any difference. This is an assignment for a data structures class and the instructor has provided the code for the implementation of the stack so I know that they are correct. Any ideas would be greatly appreciated.
The stack class was written by the instructor. There was also a file to test the stack operations and it works for ints. I just thought maybe I'm not using the stack itself correctly with char? In the instructor's test file:
#include <iostream>
#include "stack.h"
using namespace std;
int main(void)
{
Stack<int> s;
s.push(1);
s.push(2);
s.push(3);
while(!s.isEmpty() )
cout << s.pop() << endl;
return 0;
}
Like I said, everything works great if I use integers but I need it for chars. I am lost as to why I am getting this error. Thank you for your help.
Have you tried running through it with a debugger? That can help a lot, and allows you to see exactly what is going on with the memory and why your program is crashing.