Memory Fault- Core Dumped

Hi all,

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.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <cstring>
#include "ansi.h"
#include "stack.h"

using namespace std;

int main()
{
   Stack<char> operands;
   Stack<char> numbers;
   char ans, inputArray[1024];
   int option;
   string input;

   do
   {
      cout << clearScreen << cursorPosition(1,1)
           << "Which type of expression do you wish to enter?" << endl << endl
           << "1. Infix Notation" << endl
           << "2. Reverse Polish Notation" << endl << endl
           << "Option: ";
      cin >> option;

      if(option == 1)
      {
         int left = 0, right = 0;
         cout << endl << "Expression: ";
         cin >> inputArray;

         //Check for matching parentheses
         for(int i = 0; inputArray[i] != '\0'; i++)
         {
            if(inputArray[i] == '(' )
               left++;
            else if(inputArray[i] == ')' )
               right++;
         }
         if(left != right)
         {
            cout << "Syntax Error" << endl;
            cout << "Try again (Y/N): ";
            cin >> ans;
         }


         cout << inputArray[0] << endl;
         //operands.push(inputArray[0]);

         //this is the line that causes the memory fault (core dumped) error
         //cout << operands.pop() << endl;
      }
      else
      {

      }

   }while(ans == 'Y' || ans == 'y');

   return 0;
}


Is Stack your own class? Maybe one of the push and pop functions has a bug.
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.
Is it the same debugger as the gdb debugger? I've never known there was a debugger on the unix/putty terminal until recently.
Topic archived. No new replies allowed.