Search code examples
c++data-structuresstack

Segmentation Fault in Stack, Printing Weird Numbers


I keep getting a segmentation fault and the printsum() areas of my code print crazy numbers. I've tried running it through an online GDB debugger but couldn't find the error. Essentially the first input contains the number of queries and then it prompts the user to type a "type" of query for each query that determines what the program will do. Here are the types:

Type 1: A single integer follows, which you have to push in the stack.

Type 2: Two space separated integers, n and x follow. You have to push the given element x, n times in the stack.

Type 3: You have to print(in a new line) and pop the top most element in stack.

Type 4: A single integer ‘n’ follows. You have to pop the top ‘n’ integers of the stack. Also print(in a new line) the sum of the popped elements.

Type 5: You have to simply print the sum of all the elements of the stack.

Note: In case of 'n'(the number of elements to be popped) being greater than the total number of elements in the stack, return the sum of elements present in the stack.

Here is my code:

#include <iostream>
#include <stack> 

using namespace std;

stack<int> s;

void printsum() {
  int sum;
  while (!s.empty()) {
    sum += s.top();
    s.pop();
  }
  cout << sum << endl;
}

void type2(int numofPush, int numtoPush) {
  int i;
  for (i = 0; i < numofPush; ++i) {
    s.push(numtoPush);
  }
}

void type3() { s.pop(); cout << s.top() << endl; }

void type4(int singleInt) {
  int i, sum = 0;
  for (i = 0; i < singleInt; ++i) {
    sum += s.top();
    s.pop();
  }
  cout << sum << endl;
}

int main() {
  
int i, numQueries = 0, numtoPush = 0, numofPush, type = 0, singleInt;
  
  cin >> numQueries;
  for (i = 0; i < numQueries; ++i) {
  cin >> type;

  if (type == 1) {
      cin >> numtoPush;
      s.push(numtoPush);
    } else if (type == 2) {
      cin >> numofPush >> numtoPush;
      type2(numofPush, numtoPush);
    } else if (type == 3) {
      type3();
    } else if (type == 4) {
      cin >> singleInt;
      type4(singleInt);
    } else if (type == 5) {
      printsum();
    }
  }
}

And here is a sample result that happens with my code:

Input: 12 2 42 5 3 5 4 6 5 2 5 42 5 4 6 4 10 5 3 5

Output: Exited with return code -11 (SIGSEGV).

Output: 5 22144

Instead of:

5 205 30 175 385 215 50 120 5 115


Solution

  • For type 5:

    Type 5: You have to simply print the sum of all the elements of the stack.

    Since there is no way to iterate a stack, you get the sum by poping and getting stack top. To get the sum of a stack, you can copy the stack and on the sum calculation is complete, copy it back to stack s.

    So update printsum like this:

    void printsum() {
        
        stack<int> j = s;
        int sum = 0; //Be sure to init to zero
        while (!s.empty()) {
            sum += s.top();
            s.pop();
        }
        cout << sum << endl;
        s = j;
    }
    

    You will get the correct answer:

    enter image description here