0

I am supposed to print the largest element of the stack (using vector to implement stack here) whenever i encounter a query "Q" it works just fine when i run it against some sample testcases but whenever i submit the code it gives me segmentation fault.

when the query is A 10 Add 10 to stack, when query is R pop element from stack, when query is Q print the largest element in the stack

#include <stdio.h>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
using namespace std;


int main() {
   int t=0;
    cin>>t;

    for(int x=0;x<t;x++){
      printf("Case %d:\n",x+1);  
     int q=0;
        cin>>q;
        vector<int> myvec;
        vector<int> trackvec;
        int top=-1;

        for(int i=0;i<q;i++){
            string s;
            cin>>s;
            if(s=="A"){
              int num=0;
                cin>>num;
                myvec.push_back(num);
                if(i==0){trackvec.push_back(num);top++;}
                else{
                    if(num>trackvec[top]){
                     trackvec.push_back(num);   
                        top++;
                    }                   
                    else{
                        trackvec.push_back(trackvec[top]);
                        top++;
                    }
                }

            }

            else if(s=="R"){
                myvec.pop_back();
                trackvec.pop_back();
                top--;
            }
            else if(s=="Q" && top==-1){
                cout<<"Empty"<<endl;
            }
            else if(s=="Q"){
                cout<<trackvec[top]<<endl;
            }


    }
}
}
The trackvec here is to keep track of the largest element 

Sample input:
2
7
A 10
A 5
Q
A 100
Q
R
Q
6
A 5
Q
R
Q
R
R

  • 2
    This seems like a very good time to learn how to use a *debugger*. If you run the program in a debugger, it should catch the crash as it happens, and let you examine the call stack to see where in your code it happens. You will also be able to see the values of involved variables. – Some programmer dude Sep 29 '19 at 14:49
  • 2
    Welcome to SO! Please see [Why should I not include bits/stdc++](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is using namespace std considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – ggorlen Sep 29 '19 at 15:01
  • What is the *exact* input you feed to your program that results in a crash? – Employed Russian Sep 30 '19 at 03:50
  • Omg didnt expect i would get replies so soon, @Someprogrammerdude @ggorlen Thanks for the suggestions I`ll check them out! – InsaneVoice99 Sep 30 '19 at 14:03
  • @EmployedRussian I have edited the post you can check the sample inputs , but the code does work well for those sample inputs ..A x means add integer x to the list...R means remove the last inserted element and Q means to print the current largest element in the list – InsaneVoice99 Sep 30 '19 at 14:07
  • @EmployedRussian I dont know what exact input results this program to crash since the testcases are not public. – InsaneVoice99 Sep 30 '19 at 14:09

1 Answers1

0

I have edited the post you can check the sample inputs , but the code does work well for those sample inputs

Your first task should be to produce an input that causes the crash. What assumptions are you making with respect to how many As and Rs there are in the input?

Here is an input that produces segmentation fault on my system:

1
2
R
Q

The fix is hopefully obvious to you now.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362