Note that there are some explanatory texts on larger screens.

plurals
  1. POProgramming error in allocating new stack
    primarykey
    data
    text
    <p>I am writing a program to implement a stack which works like a real world stack means it topples when the size of a stack reaches threshold and therefore need to create a new stack for inserting that new element.</p> <p>Below is my program for this:</p> <pre><code>#include &lt;iostream&gt; #include&lt;vector&gt; #include&lt;stack&gt; using namespace std; class stack_of_plates { vector&lt;stack&lt;int&gt; &gt; stacks; unsigned int stack_size; public: stack_of_plates(unsigned int size=100) { stack_size=size; } void push(int data) { if(stacks.empty()) { stack&lt;int&gt; *sptr= new stack&lt;int&gt;; //on debugging Segmentation fault at thisline stacks.push_back(*sptr); } vector&lt;stack&lt;int&gt; &gt;::iterator it=stacks.end(); if(it-&gt;size()==stack_size) { stack&lt;int&gt; *sptr= new stack&lt;int&gt;; //on debugging Segmentation fault at thisline stacks.push_back(*sptr); } it-&gt;push(data); } void pop() { if(stacks.empty()) { cout&lt;&lt;"\nEmpty Stack"; return ; } vector&lt;stack&lt;int&gt; &gt;::iterator it=stacks.end(); if(it-&gt;empty()) { it--; } it-&gt;pop(); } int top() { if(stacks.empty()) { cout&lt;&lt;"\nEmpty Stack"; return 0; } vector&lt;stack&lt;int&gt; &gt;::iterator it=stacks.end(); if(it-&gt;empty()) { it--; } return it-&gt;top(); } }; int main() { stack_of_plates ss; ss.push(1); ss.push(2); cout&lt;&lt;ss.top(); return 0; } </code></pre> <p>On compiling it gives no error or warning. However program terminates with unusual error. On debugging its giving segmentation fault error indicating problem in allocating new stack. Kindly help me how should i change my code while allocating the new stack. Please help me removing this error.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload