Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading