59032

C++ STL stack pop operation giving segmentation fault

Question:

I implemented a programming question from <a href="http://www.geeksforgeeks.org/next-greater-element/" rel="nofollow">this link</a> in C++ but I am getting a segmentation fault in the pop() operation with my code. I am fairly new to C++ and can not seem to find the error myself.

#include<iostream> #include<stack> using namespace std; void printNge(int *arr); int main() { int arr[] = {1,4,2,6,3,8,7,2,6}; printNge(arr); return 0; } void printNge(int *arr) { stack<int> st; st.push(arr[0]); for(int i=1; i<9;i++) { while((st.top() < arr[i]) && (!st.empty())) { cout << "Element is:" << st.top() << " NGE is:" << arr[i] << endl; cout << "Removing element: " << st.top() << endl; st.pop(); } cout << "Pushing element: " << arr[i] << endl; st.push(arr[i]); } while(!st.empty()) { cout << "Element is:" << st.top() << " NGE is:" << -1 << endl; st.pop(); } }

Thanks for the help.

Answer1:

This line

while((st.top() < arr[i]) && (!st.empty())) {

is what is causing the segfault. You have to check the stack for being empty before you try to access top, as caling top on empty stack invokes UB.

Answer2:

Calling pop_back on an empty container is undefined. std::stack uses std::deque by default, see <a href="http://en.cppreference.com/w/cpp/container/deque/pop_back" rel="nofollow">std::deque::pop_back</a>:

Recommend

  • Laravel Undefined Variable - Using isset()
  • Call JQuery function From Controller
  • Is there a way to continuously pipe data from Azure Blob into BigQuery?
  • Get a class as javax.lang.model.element.Element in junit tests
  • Is there a generic TableModel we can use in JTables?
  • std::future returned from std::async hangs while going out of scope
  • set clipboard content in any browser
  • Unreadable characters displaying in ASP.NET MVC
  • Eloquent Javascript: Can't understand how the number value is determined in the sum function
  • Java anti fillRect (fill everything outside of said rectangle)
  • How to populate a Mutation for a different Cassandra table in a trigger
  • Angular not getting response when it's a non-200
  • C# mvc3 redirect sitemap.xml to controller action
  • Covert RFC3339 DateTime to Date in java [duplicate]
  • Retrieve 3rd MAX salary in Hive
  • How do I detect if an email client is configured on an Android device?
  • Interactive labeling of images in jupyter notebook
  • Firebase suddenly reports invalid signature
  • ggplot2 facet_grid with distinct x-axis labels using facet_grid
  • Can someone explain how Yii minimizing assets is supposed to work on Heroku?
  • Directed graph linear algorithm
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • PHP: Need to close STDIN in order to read STDOUT?
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • Multiplying polynomials/simplifying like terms
  • What Exception is thrown on timeout?
  • Update cell query for Excel ADO from Delphi
  • Codeigniniter insert data through models and controller
  • VLOOKUP in IMPORTRANGE
  • Sign a Pdf using custom digital signature in Java
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • PHP Permalinks.. how to change?
  • media foundation H264 decoder not working properly
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Spring Boot not autowiring @Repository
  • Running R's aov() mixed effects model from Python using rpy2
  • Access to a Matlab gui from the web
  • JavaScript RegExp Replace
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}