Seg Fault error when returning to function

My program should convert the invitation from infix to postfix. So far, through the debugger and other methods, I have found the exact point at which I segfault, but do not understand why.

Here is my code:

Here itop.h:

using namespace std; #include <cstdlib> #include <iostream> class sNode{ public: char data; sNode *next; }; class stack{ public: sNode *head; void push (char); sNode pop(); int rank(char); stack() { cout << "Initiliazing stack." << endl; } }; 

This is my itop.cpp file:

  #include "itop.h" void stack::push (char a) { // cout << "Pushing " << a << endl; sNode *sn; sn = new sNode; sn->data = a; sn->next = head; head = sn; } sNode stack::pop() { // cout << "Popping stack." << endl; sNode *sn; sn = head; head = head->next; return *sn; } int stack::rank(char x) { int num = 0; // cout << "Checking rank." << endl; if(x == '\0') { num = 1; // cout << "Checking for null" << endl; return num; } else if(x == '+' || x == '-') { num = 2; // cout << "Checking if + or -" << endl; return num; // cout << "After return." << endl; } else if(x == '*' || x == '/') { num = 3; // cout << "Checking for * or /" << endl; return num; } else cout << "Error! Input not valid!" << endl; } 

And here main.cpp:

 using namespace std; #include <iostream> #include <cstdlib> #include <cstring> #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 << "infix_to_postfix> "; cin >> temp1; if(strcmp(temp1, "quit") == 0) { return 0; } if(strcmp(temp1, "convert") != 0) { cout << "Error! Invalid command." << endl; } cin >> temp2; if(strcmp(temp1, "convert") == 0) { for(int i=0; i<sizeof(temp2); i++) { if(isdigit(temp2[i])) { cout << atoi(&temp2[i]); } else if(s.rank(temp2[i]) < s.rank(s.head->data)) { sNode temp = s.pop(); cout << temp.data; } else { s.push(temp2[i]); } } } else { cout << "Error! Command not supported." << endl; } }while(strcmp(temp1, "quit") != 0); return 0; } 

Function called in

 else if(s.rank(temp2[i]) < s.rank(s.head->data)) 

And the problem is here:

  else if(x == '+' || x == '-') { num = 2; // cout << "Checking if + or -" << endl; return num; // cout << "After return." << endl; } 

In particular, right before num returns, I get the error "Segmentation failed (core dumped)". I used gdb, and all I know is right after "Check if + or -" I see "$ 1 = 2". I do not quite understand what this means, but this is what I want to return.

Thank you for your help.

+4
source share
2 answers

Fix 1: Write the correct constructor.

  stack() { head=NULL; cout << "Initiliazing stack." << endl; } 

Fix 2: write an optional method to check if empty is empty.

 int stack::empty() { if(head == NULL) return true; else return false; } 

Fix 3: check to see if the stack has stacked before using stack data.

 else if(!s.empty() && s.rank(temp2[i]) < s.rank(s.head->data)) { ... } 

Fix 4: Fix the rest of the code logic.

0
source

There are many errors in the code. Stack implementation error. push() for example, only sets head over and over. This causes your stack class to hold only one element. next never installed on anything, so it contains random garbage. Next, you have this:

 for(int i=0; i<sizeof(temp2); i++) 

sizeof(temp2) does not give you the number of characters of a string temp2 points to. It gives you the size of the temp2 pointer. In addition, you end reading s.head from an empty stack, which will be a pointer to random garbage. At this point, all bets fail, of course. You cannot expect anything but an accident and burning.

+1
source

Source: https://habr.com/ru/post/1440909/


All Articles