0

I performed the pop operation on S[position] stack and it's giving segment fault but the top operation giving the result without any error and the empty check also performed before the pop operation.

Can't figure out what is causing the problem.

#include<iostream>
#include<stack>
#include<stdlib.h>
using namespace std;

int main(){
    int bar,position;
    cout<<"Enter the number of bars: ";cin>>bar;
    cout<<"Enter the position of the bar where you want to place: ";cin>>position;
    position = position - 1;
    stack<int> S[bar];
    for(int i = bar; i>0; i--){
        S[0].push(i);
    }

    S[0].pop();

    for(int i = 1; i<bar; i++){
        if(i == position){
            S[i].push(1);
            continue;
        }
        S[i].push(S[0].top());
        S[0].pop();
    }

    for(int i = 1; i< bar; i++){
        if(S[i].top() == 2){
            if(!S[position].empty()){
                cout<<S[position].top();
                S[position].pop(); //This line generating segment fault
            }
        }
    }

}

Output:

Enter the number of bars: 3
Enter the position of the bar where you want to place: 2
Segmentation fault (core dumped)
SID
  • 29
  • 5
  • 1
    What was your input ot this function ? I ask because as near as I can see, `cout< – WhozCraig Nov 11 '19 at 14:13
  • 3
    `stack S[bar];` is not valid in standard, please don't use it. Try moving the call to `top` inside the `if` as well, – ChrisMM Nov 11 '19 at 14:13
  • Calling it in the `if` isn't even sufficient, if you remove one element from the stack before. You have to check for non-emptyness again after the `pop`. – n314159 Nov 11 '19 at 14:15
  • @WhozCraig when I comment out the if block then the top operation giving no error and the program run without an error but when I add teh pop() operation then it's giving segment fault. – SID Nov 11 '19 at 14:21
  • @SID great, but that makes absolutely no difference to my comment's observation. The code you had posted had the flaw i described. The logic of your program outside of that really isn't a concern to me. As it was posted, if the stack at `S[position]` is either (a) empty, or (b) has one element (that is therefore popped because now the if-test holds true), you're resulting reference to `top()` in the code `cout< – WhozCraig Nov 11 '19 at 14:26
  • @WhozCraig I used that just to check if the top operation is working or not. I tried top() operation before the pop() in the if statement but the same error I got. I have updated the code in my post also. – SID Nov 11 '19 at 14:32
  • @I just ran your updated code with your proposed input. no violations. In short, I simply don't believe your prior comment ([and neither does ideone](https://ideone.com/uIJIe3)). – WhozCraig Nov 11 '19 at 14:36
  • @WhozCraig yup the code is running fine on idone. Then why it's not running on my pc . I am running gcc 7.4 – SID Nov 11 '19 at 14:47
  • Change the input to `4` and `3` and it doesn''t work as well. – Ted Lyngmo Nov 11 '19 at 14:58
  • @WhozCraig Thank you for your help. Now it's working I missed some boundary values. – SID Nov 11 '19 at 15:13

1 Answers1

0

1.You can use one library - bits/stdc++.h. This lib contains almost everyone lib.

    #include <bits/stdc++.h>

2.If bars=b, stack have k elements, 0 - k-1, and you go in 1. You must go on 0, or make stack with k+1 elements.

stack<int> s[k+1] and for(long long i=1 ; i<=bar ; i++);

or

for(i=0 ; i<bars ; i++)

3.The real error - the one you are looking for is not on the order in which you think. The error is on line when you make check for its value. line:

if(S[i].top() == 2)

The error is here, because you are not sure are this stack empty. You check for stack[position]. If stack is empty and you get .pop() or .top() the compiler give you Segmentation fault (core dumped). In my opinion you are confused about where to write i and position, but check -!S[i].empty() must be before if(S[i] == 2). Or you just need to check the stack with index i if it's empty. After the changes, the code look like this:

    #include<bits/stdc++.h>

using namespace std;

int main(){ int bar,position;

cout<<"Enter the number of bars: ";cin>>bar;
cout<<"Enter the position of the bar where you want to place: ";cin>>position;
position = position - 1;
stack<int> S[bar];
for(int i = bar; i>0; i--){
    S[0].push(i);
}

S[0].pop();

for(int i = 0; i<bar; i++){
    if(i == position){
        S[i].push(1);
        continue;
    }
    S[i].push(S[0].top());
    S[0].pop();
}
    /*
    for(int i = 0; i < bar; i++){
            if( !S[i].empty() ){
                    if(S[i].top() == 2){
                            cout<<S[i].top();
                             S[i].pop();
                   }
            }
    }
     OR
     */
for(int i = 0; i < bar; i++){
    if( !S[i].empty() ){
            if(S[i].top() == 2){///this line gave you a segmentation fault (core dumped)
                    if(!S[position].empty()){
                            cout<<S[position].top();
                             S[position].pop(); 
                    }
            }
    }
}
    return 0;

}

  • *"use one library - bits/stdc++.h"* See [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h/31816096) – HolyBlackCat Dec 10 '19 at 19:55