How can a stack underflow happen in C++?

后端 未结 4 1526
半阙折子戏
半阙折子戏 2020-12-25 15:06

What is a simple example in C++ that causes a stack underflow in the case of invoking and returning from method calls?

I am familiar with the calling convention, i.e

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-25 15:54

    I am not sure if you are talking of the data structure stack and the underflow problem in it or something else. As far as the stack(data structure) underflow problem is concerned here is a explanation.

    stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: push, pop and stack top.

    The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack.

    A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into underflow state (It means no items are present in stack to be removed).

    The stack top operation gets the data from the top-most position and returns it to the user without deleting it. The same underflow state can also occur in stack top operation if stack is empty.

    Consider a stack implementation example:

    template  class Stack 
    {
    public:
        bool isEmpty() const;
        size_t size() const;
        Item pop();
        void push(const Item& it);
    private:
    
    };
    

    Now consider the following operations being performed on this stack.

    C++ command                      resulting stack
    ------------------------------------------------
    Stack S;
                                      _____ (empty stack of ints)
    
    
    
    S.push(7);                            
                                      | 7 |  <-- top
                                      -----
    
    S.push(2);                            
                                      | 2 |  <-- top 
                                      | 7 |
                                      -----
    
    S.push(73);                           
                                      |73 |  <-- top 
                                      | 2 |
                                      | 7 |
                                      -----
    
    S.pop();                           
                                      | 2 |  <-- top
                                      | 7 |                    -----
    S.pop();      
                                      -----
    S.pop();                           
                                      | 7 |  <-- top
                                      -----
    S.pop();                           
                                      -----  (empty)
    
    S.pop();                           
                        ERROR "stack underflow"
    

提交回复
热议问题