Solved

Stacks container

Posted on 2008-06-20
7
208 Views
Last Modified: 2010-04-01
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
Comment
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
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 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 ();
}

Open in new window

0
 

Author Comment

by:jschmuff
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 45

Expert Comment

by:Kent Olsen
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:jschmuff
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

by:jschmuff
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";
	}
 
}

Open in new window

0
 

Author Comment

by:jschmuff
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 45

Expert Comment

by:Kent Olsen
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This video teaches viewers about errors in exception handling.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question