title: Seg Fault at return statement in function
body: <p>My program is supposed to convert a prompt from infix to postfix. So far, through a debugger and various other methods, I have located the exact point at which I segfault, but don't understand why. </p>
  
  <p>Here's my code:</p>
  
  <p>Here's itop.h:</p>
  
  <pre><code>using namespace std;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  
  class sNode{
      public:
         char data;
         sNode *next;
  };
  
  class stack{
      public:
          sNode *head;
          void push (char);
          sNode pop();
          int rank(char);
          stack()
          {
          cout &lt;&lt; "Initiliazing stack." &lt;&lt; endl;
          }
  };
  </code></pre>
  
  <p>This is my itop.cpp file:</p>
  
  <pre><code>    #include "itop.h"
  
      void stack::push (char a)
      {
              // cout &lt;&lt; "Pushing " &lt;&lt; a &lt;&lt; endl;
              sNode *sn;
              sn = new sNode;
              sn-&gt;data = a;
              sn-&gt;next = head;
              head = sn;
      }
  
      sNode stack::pop()
      {
              // cout &lt;&lt; "Popping stack." &lt;&lt; endl;
              sNode *sn;
              sn = head;
              head = head-&gt;next;
              return *sn;
      }
  
      int stack::rank(char x)
      {
              int num = 0;
              // cout &lt;&lt; "Checking rank." &lt;&lt; endl;
              if(x == '\0')
              {
                      num = 1;
                      // cout &lt;&lt; "Checking for null" &lt;&lt; endl;
                      return num;
              }
              else if(x == '+' || x == '-')
              {
                      num = 2;
                      // cout &lt;&lt; "Checking if + or -" &lt;&lt; endl;
                      return num;
                      // cout &lt;&lt; "After return." &lt;&lt; endl;
              }
              else if(x == '*' || x == '/')
              {
                      num = 3;
                      // cout &lt;&lt; "Checking for * or /" &lt;&lt; endl;
                      return num;
              }
              else
                      cout &lt;&lt; "Error! Input not valid!" &lt;&lt; endl;
      }
  </code></pre>
  
  <p>And here's main.cpp:</p>
  
  <pre><code>using namespace std;
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;cstring&gt;
  #include "itop.h"
  
  int main()
  {
  char *temp1;            //Instantiating variables.
  char *temp2;
  temp1 = new char[20];
  temp2 = new char [20];
  stack s;
  do              //Checking commands.
  {
      cout &lt;&lt; "infix_to_postfix&gt; ";
      cin &gt;&gt; temp1;
      if(strcmp(temp1, "quit") == 0)
      {
          return 0;
      }
      if(strcmp(temp1, "convert") != 0)
      {
          cout &lt;&lt; "Error! Invalid command." &lt;&lt; endl;
      }
      cin &gt;&gt; temp2;
      if(strcmp(temp1, "convert") == 0)
      {
          for(int i=0; i&lt;sizeof(temp2); i++)
          {
              if(isdigit(temp2[i]))
              {
                  cout &lt;&lt; atoi(&amp;temp2[i]);
              }
              else if(s.rank(temp2[i]) &lt; s.rank(s.head-&gt;data))
              {
                  sNode temp = s.pop();
                  cout &lt;&lt; temp.data;
              }
              else
              {
                  s.push(temp2[i]);
              }
          }
      }
      else
      {
          cout &lt;&lt; "Error! Command not supported." &lt;&lt; endl;
      }
  }while(strcmp(temp1, "quit") != 0);
  
  return 0;
  }
  </code></pre>
  
  <p>The function is called at</p>
  
  <pre><code>else if(s.rank(temp2[i]) &lt; s.rank(s.head-&gt;data))
  </code></pre>
  
  <p>And the problem is in here:</p>
  
  <pre><code>            else if(x == '+' || x == '-')
              {
                      num = 2;
                      // cout &lt;&lt; "Checking if + or -" &lt;&lt; endl;
                      return num;
                      // cout &lt;&lt; "After return." &lt;&lt; endl;
              }
  </code></pre>
  
  <p>Specifically right before return num, I get "Segmentation fault (core dumped)" error message. I have used gdb and all I know is that right after "Checking if + or -" I see "$1 = 2". I'm not quite sure what that means, but it is what I want to return. </p>
  
  <p>Thank you for your help.</p>