Expiring Todayâ€”Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Stacks container

Posted on 2008-06-20
Medium Priority
211 Views
Can I get a real simple example of stacks I don't know using a simple variable like names or numbers anything that is easy to understand the less code the better to help me understand.
0
Question by:jschmuff
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3

LVL 46

Accepted Solution

Kent Olsen earned 2000 total points
ID: 21836177

Hi jschmuff,

The stack is simply memory that the program uses for temporary variables.

The program manages it with something call the Stack Pointer.  It typically contains the address on the stack where current function's variables reside.  It's important to know that internally, items on the stack are referenced by address (the Stack Pointer) + an offset.  (Your program may just use integer or character values.)

An example might help.  :)

In the example below, when the program starts, the stack pointer initially points to the first item in the parameter list.  In this case argc.

When main calls Function1, the stack pointer is advanced by 8 bytes (4 for the integer argc, and 4 for the pointer argv).  The stack pointer actually advances by more than 8 bytes as there are some housekeeping values that must be stored, but let's keep it simple.

When Function1 calls Function2, the stack pointer is advanced by 24 bytes.  4 for the integer A, and 20 for the character array B.  If Function2 calls another function, the Stack Pointer will advance 12 bytes.  4 for integer C, 4 for integer D, and 4 for integer E.

The key item so far is that parameters AND variables declared in functions are all placed on the stack.

And just as importantly, when a function returns to the function that called it, the Stack Pointer is moved back to where it was before the function was called.  No matter how many functions are called, or their complexity the execution of a function uses the same value for the Stack Pointer from start to finish.  If the function is recursive, so that it calls itself, each execution has its own value for the Stack Pointer.  It is advanced when another function  is called and put back whenever the new function issues a return (or returns implicitly).

Good Luck,
Kent

``````Function1 ()
{
int     A;
char  B[20];
//
Function2 (A);
}
//
Function2 (int C)
{
int D;
int E;
}
//
main (int argc, char **argv)
{

Function1 ();
}
``````
0

Author Comment

ID: 21836200
I guess I am really confused in this book I have it shows all the the functions in a class and does pretty much all of it I just want to start out small something really simple if I can, if this stack can be extremely simple like in one .cpp fle not having classes. Can I get an example of what the push functions would look like?
0

LVL 46

Expert Comment

ID: 21836222
Howdy....

Don't worry about classes.  The stack doesn't change because a class object is instantiated.  It changes because a function is executed and the function's parameters and variables need to be preserved while another function executes.

Keep in mind that when a class is instantiated the constructor method is often executed.  This is essentially the same as calling a function (even though it happens automatically) and the stack is adjusted for the execution of the constructor.

But it is the execution that causes the stack to change, not the class itself.

Kent
0

Author Comment

ID: 21836244
I am still confused at creating a function for a stack, I am looking at these examples in my book and it is all more classes and I have been looking online and only see instances of class stacks which is the most confusing part for me, because of all the code that is jumbled together it is hard to make out what is what. I good example of a function or a nice tutorial page would be great I cant find anything.
0

Author Comment

ID: 21836287
This is what I have so far I dont get why it is only showing the last name I enter.

here is my code:

``````#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main()
{
stack<string> stackName;

string name;

int SIZE = 0;
int count = 0;

cout << "Enter how big the stack is: ";
cin >> SIZE;
cout << endl;

count = SIZE;

for (int i = 0; i < count; i++)
{
cout << "Enter name to add to stack: ";
cin >> name;
stackName.push(name);
cout << endl;
}
for (int i = 0; i < count; i++)
{
cout << "Pushing: " << stackName.top() << "\n";
}

}
``````
0

Author Comment

ID: 21836393
Ok I have it all figured out how do I display what is in a stack just by using stack<int> stackInt;
0

LVL 46

Expert Comment

ID: 21837447
Ok.  I was describing the program stack.  It works similarly to any other stack, but there is some special casing as the programmer has pseudo control over it, not absolute control.

The top() method returns the top item in the stack.  It is generally the only item on the stack to which you'll have access.  To get the next item on the stack, you need to make it the top item.  You do that by calling pop(), which discards the then-current item on the stack.

Good Luck,
Kent
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there isâ€¦
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a â€¦
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month11 days, 7 hours left to enroll