Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Simple stack implementation

Reply
Thread Tools

Simple stack implementation

 
 
Thomas Ibbotson
Guest
Posts: n/a
 
      09-19-2005
I've been going through a tutorial on C++ and I've come across what I
regard as some strange behaviour. As part of the tutorial on classes a
simple stack is implemented with functions push() and pop().

I test this stack using:
stack a_stack; // Create an instance of stack

// Push some data onto the stack
a_stack.push(1);
a_stack.push(2);
a_stack.push(3);

// Now pop it off again and see what we get
cout << a_stack.pop() << " " << a_stack.pop() << " " <<
a_stack.pop() << endl;

// Refill the stack
a_stack.push(1);
a_stack.push(2);
a_stack.push(3);

// Try it a different way
cout << a_stack.pop() << endl;
cout << a_stack.pop() << endl;
cout << a_stack.pop() << endl;

The resulting output is:
1 2 3
3
2
1

Where I would have expected:
3 2 1
3
2
1

Can someone tell me what is going on?

Thanks,
Tom
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-19-2005
Thomas Ibbotson wrote:
> I've been going through a tutorial on C++ and I've come across what I
> regard as some strange behaviour. As part of the tutorial on classes a
> simple stack is implemented with functions push() and pop().
>
> I test this stack using:
> stack a_stack; // Create an instance of stack
>
> // Push some data onto the stack
> a_stack.push(1);
> a_stack.push(2);
> a_stack.push(3);
>
> // Now pop it off again and see what we get
> cout << a_stack.pop() << " " << a_stack.pop() << " " <<
> a_stack.pop() << endl;


This is a single expression where arguments to the last 'operator <<' are
evaluated in _unspecified_ order.

> [...]


V
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      09-20-2005
"Thomas Ibbotson" <> wrote in message
news:dgn2en$g27$
> I've been going through a tutorial on C++ and I've come across what I
> regard as some strange behaviour. As part of the tutorial on classes a
> simple stack is implemented with functions push() and pop().
>
> I test this stack using:
> stack a_stack; // Create an instance of stack
>
> // Push some data onto the stack
> a_stack.push(1);
> a_stack.push(2);
> a_stack.push(3);
>
> // Now pop it off again and see what we get
> cout << a_stack.pop() << " " << a_stack.pop() << " " <<
> a_stack.pop() << endl;
>
> // Refill the stack
> a_stack.push(1);
> a_stack.push(2);
> a_stack.push(3);
>
> // Try it a different way
> cout << a_stack.pop() << endl;
> cout << a_stack.pop() << endl;
> cout << a_stack.pop() << endl;
>
> The resulting output is:
> 1 2 3
> 3
> 2
> 1
>
> Where I would have expected:
> 3 2 1
> 3
> 2
> 1
>
> Can someone tell me what is going on?
>
> Thanks,
> Tom


To better understand the output, run the following code:

int main()
{
stack a_stack; // Create an instance of stack
int a, b, c; // used for recording results of pop operations

// Push some data onto the stack
a_stack.push(1);
a_stack.push(2);
a_stack.push(3);

// Now pop it off and see what we get
cout << (a=a_stack.pop()) << " " << (b=a_stack.pop()) << " "
<< (c=a_stack.pop()) << endl;

cout << "a is " << a << endl;
cout << "b is " << b << endl;
cout << "c is " << c << endl;

// Refill
a_stack.push(1);
a_stack.push(2);
a_stack.push(3);


// Pop it off a different way
cout << endl;
cout << (a=a_stack.pop()) << endl;
cout << (b=a_stack.pop()) << endl;
cout << (c=a_stack.pop()) << endl;

cout << "a is " << a << endl;
cout << "b is " << b << endl;
cout << "c is " << c << endl;
return 0;
}

The output is:

1 2 3
a is 1
b is 2
c is 3

3
2
1
a is 3
b is 2
c is 1

For the first (anomalous) result, what the output tells you is that the
numbers are being output in left-to-right order, as you would expect.
However, prior to doing the outputting, the program is making the pop()
calls in the reverse order to what you expect. Specifically, it is doing
them in the following order:

c=a_stack.pop()
b=a_stack.pop()
a=a_stack.pop()

The reason for this is that an implementation is free to make these
evaluations in any order it wants since all pop() functions are part of the
same expression.

For the second version, the semi-colon at the end of each line creates a
"sequence point" which forces the evaluation of the functions up to that
point. This forces evaluation in the expected order:

a=a_stack.pop()
b=a_stack.pop()
c=a_stack.pop()


--
John Carson

 
Reply With Quote
 
Thomas Ibbotson
Guest
Posts: n/a
 
      09-20-2005
Thanks for the explanations, I'll keep it in mind for future reference.
Tom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
Why stack overflow with such a small stack? Kenneth McDonald Ruby 7 09-01-2007 04:21 AM
"stack level too deep"... because Threads keep their "starting" stack Sam Roberts Ruby 1 02-11-2005 04:25 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57