Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Stacks container

Posted on 2008-06-20
7
Medium Priority
?
213 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
  • 4
  • 3
7 Comments
 
LVL 46

Accepted Solution

by:
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 ();
}

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 46

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 46

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

885 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