2

I wrote a small C++ program which implements basic operations of stack using linkedLists in C++. While testing that program I observed a strange issue with cout. In main function,

int main()
{
     stack *MainStack=new stack();
     MainStack->push(1);
     MainStack->push(2);
     std::cout<<MainStack->pop();
     std::cout<<MainStack->pop();
}

The output is 21 and when main function is

int main()
{
    stack *MainStack=new stack();
    MainStack->push(1);
    MainStack->push(2);
    std::cout<<MainStack->pop()<<MainStack->pop();
}

The output is 12.

What is the reason for that error? Can anyone give me reason for this error.

My C++ program is

#include<iostream>
class stack
{
    int value;
    stack *next;
public:
    stack()
    {
        value=0;
        next=NULL;
    }
    stack(int data)
    {
        value=data;
        next=NULL;
    }
    void push(int data)
    {
        stack *temp;
        temp=next;
        next=new stack(data);
        next->next=temp;
        value++;
    }
    int pop()
    {
        int data;
        if(next==NULL)
        {
            std::cout<<"Underflow\n";
            return -1;

        }
        stack *temp=next;
        next=next->next;
        data=temp->value;
        delete(temp);
        value--;
        return data;
    }
    int top()
    {
        return next->value;
    }
    bool isStackEmpty()
    {
        if(next==NULL)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    int getCount()
    {
        return value;
    }
};
int main()
{
    stack *MainStack=new stack();
    MainStack->push(1);
    MainStack->push(2);
    std::cout<<MainStack->pop()<<MainStack->pop();
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Abhinav Konda
  • 225
  • 1
  • 11
  • 1
    I fail to see why you use `new` and pointers in `main`. Just use `stack MainStack;` and `.` instead of `->`. Then you don't have a missing `delete` like you do now. – chris Jul 27 '14 at 18:14
  • @chris Yea thats true and BTW why did they overload << and >> operators for cin and cout. I mean why did they choose bit wise operators for that? – Abhinav Konda Jul 27 '14 at 18:17
  • It is unspecified which `pop()` is evaluated first. Therefore, which `pop` returns 1 and which pop returns 2 is unspecified and either is a valid outcome. – chris Jul 27 '14 at 18:20
  • @chris Yea thats true and BTW why did they overload << and >> operators for cin and cout. I mean why did they choose bit wise operators for that? As they overloaded bit wise operators and its associativity is from right to left I got this issue. – Abhinav Konda Jul 27 '14 at 18:22
  • I have no idea why the bitwise operators were used. The most common explanation I see is that data flows into or out of the stream. I don't know if that was what was going through the minds of those responsible for the decision. I'm almost sure a low operator precedence was one reason. Anyway, associativity has nothing to do with your problem. All associativity does is group it like `(std::cout << MainStack->pop()) << MainStack->pop();`. The order in which each operand is evaluated is unaffected. How the values are used is just like how it reads. But computing those values is not. – chris Jul 27 '14 at 18:23
  • 1
    Associativity has nothing to do with it, and neither does operator precedence. – Lightness Races in Orbit Jul 27 '14 at 18:25
  • @Lightness Races in Orbit Then what do you think is the reason? – Abhinav Konda Jul 27 '14 at 18:26
  • @AbhinavKonda: Order of operand evaluation. – Lightness Races in Orbit Jul 28 '14 at 09:20

0 Answers0