Solved

Stacks container

Posted on 2008-06-20
7
201 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 45

Accepted Solution

by:
Kdo 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:Kdo
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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:Kdo
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
wordsFront challenge 8 70
sumHeights2  challenge 7 78
groovy example issue 10 70
Create a path if not exists 7 47
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
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…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now