473,836 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple stack implementation

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
Sep 19 '05 #1
3 2454
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
Sep 19 '05 #2
"Thomas Ibbotson" <th************ *@oriel.ox.ac.u k> wrote in message
news:dg******** **@news.ox.ac.u k
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

Sep 20 '05 #3
Thanks for the explanations, I'll keep it in mind for future reference.
Tom
Sep 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
30107
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
17
5055
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
10
8351
by: Shuo Xiang | last post by:
Greetings: I know that variables declared on a stack definitely does not reside in heap space so there is only a very limited amount of stuff that you can store in the stack of a function. But what about the global space? Is that the same as the heap space or is that still a form of special stack? Because once I've seen someone declare a 1 megabyte char array in global space like this: char s;
20
3507
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
16
4454
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
9
3176
by: Roman Mashak | last post by:
Hello, I'm confused about heap and stack memories management in C language. Most books explain that local stack variables for each function are automatically allocated when function starts and deallocated when it exits. In contrast, malloc() always takes memory in the heap. Now, let's consider the code as follows: int get_buffer() {
3
2933
by: NewLine | last post by:
Hi If I have a function: std::vector<intmy_rbv() { std::vector<intx; // some stuff return x; }
87
5588
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first place? I can see two definite disadvantages with this: 1) deeply nested recursive calls to a function (especially if it defines
62
2904
by: jt | last post by:
hello everyone.. int fun() { /* anything */ } int main(void) { fun(); }
0
9825
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9673
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10859
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10602
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9388
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4463
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.