Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Stacks container

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
jschmuff
Asked:
jschmuff
  • 4
  • 3
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
jschmuffAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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 Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
jschmuffAuthor Commented:
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
 
jschmuffAuthor Commented:
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
 
jschmuffAuthor Commented:
Ok I have it all figured out how do I display what is in a stack just by using stack<int> stackInt;
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now