Jump to content
  • Advertisement
Sign in to follow this  
redirectionkid

the stack?

This topic is 4882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, heres the deal: I'm a beginner... and I read -alot- of tutorials. Mostly about stuff I don't even understand, so I thought I'd try to "start over" from the ground up, and try to not only learn coding techniques, but to implement them into something useful this time around. But I've come across several terms which are the source of some confusion. More specifically, I've seen the word "stack" thrown around multiple times. I'm not exactly sure what it is used for, and how it would be applied to C/C++ and game programming. All I really know, is that it has something to do with memory, and push and pop and what-not. I've searched google, and I've read a wide range of articles on this site, yet nothing really nips the topic in the bud. I'm looking for a decent article that fully explains the topic, yet I haven't really found any. If anyone could direct me to one, I'd appreciate it. Or perhaps you'd like to enlighten me yourself? If so, then by all means, feel free to do such. If it makes any difference, I've been meddling around with C/C++ for a couple years now, so I'd say I have a pretty solid understanding of most of the basics. But not quite solid enough. Thanks, -Darin

Share this post


Link to post
Share on other sites
Advertisement
The stack is a portion of memory which static (non-pointer) variables are allocated on.

It's called the stack because that is essentially what it is. When something is added to the stack, it must go on the top. This is called "pushing" the item onto the stack. The only thing that can be removed from the stack is the topmost item. This is called "popping" off the topmost item.

Dynamically allocated variables (pointers) are placed into a portion of memory called the "heap". The heap is quite a bit different from the stack. Most important is that the position at which a new item will be added it totally random. Therefore, the heap permits random-access. You can add something anywhere, and remove something from anywhere.

Though this has it's advantages, it leads to several problems including slower run-time performance and heap fragmentation.

Share this post


Link to post
Share on other sites
Whenever you declare a variable like this:

int foo;

or this:

auto int bar;

You're creating it on the stack (auto isn't ever necessary, it just means to put it on the stack)

The stack is a place in memory about two megabytes large (I think), where scoped variables go. It's very fast. It's fast because it's staticalready known what variables are needed--there can be no dynamic programming because you have to initialize all variables at compile time. With the stack, there is no random access. Things are stored in order.

The heap and the free-space are two other areas or putting memory. Using the new keyword and the malloc() functions allow you to access them (I don't think they're the same thing, but I'm not sure).

I hope this helps.

Edit: oops, nilkn beat me.

Share this post


Link to post
Share on other sites
And just to make matters worse...Stack is also an Abstract Datatype (ADT).

hmm...Abstract datatype ?

A description of what a datatype does, without describing how (implementation).

Share this post


Link to post
Share on other sites
On the Windows platform:

The OS distributes execution time on the CPU('s) by scheduling threads. Each process has at least one thread. A thread can start an other thread in the same process. Threads in the same process share data that is allocated on the Heap (using malloc, new, calloc) and data that is in the Data segment (globals, literals and constants) Each thread has it's own memory to store local variables and parameters: this is called the Stack. Also: each thread keeps it's own CPU register values.

Cheers

Share this post


Link to post
Share on other sites
As MichaelWeber said, it also refers to a data structure.
Works pretty much like a real-world stack of, well, something, dishes, maybe? Or books or CD's, or anything else. You have two possible operations, push and pop. Push puts a new item on top of the stack. Pop takes the top one off. You can't just grab something from the middle of the stack, you have to deal with the top of it only. I bet you have a stack of CD's next to the computer. You can't just grab the bottom one, without taking the top ones off first. That's a stack. [wink]

The memory area the others explained about works in the same way (hence the name). Every new function pushes data onto the stack, and when the function ends, they're popped off, leaving the stack as it looked before the function was called.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
So if I make a function that takes a pointer as an argument. will that pointer be deleted after the function ends? or do I have to delete it myself at the end of the function.

-Mark

Share this post


Link to post
Share on other sites
Okay, I think I have a better idea of things. However I'm not quite sure here:

Quote:
Original post by Spoonbender
Every new function pushes data onto the stack, and when the function ends, they're popped off, leaving the stack as it looked before the function was called.


Do all variables within the function utilize the stack by default? If so, how could one access a variable that is defined before another variable, without "popping off" the previous variable, and losing that data?

Or perhaps the stack only applies to constants? Nilkn mentioned something about the heap, and static/dynamic data... is this what he/she is referring to?

Share this post


Link to post
Share on other sites
I think that learning a lower level language(ie, assembly) would greatly facillitate understanding of the stack.

Share this post


Link to post
Share on other sites
For beginning programming all you really need to know is that the stack is where your non-dynamic variables are stored, and the heap is where your dynamic allocation takes place.

That's when your stack is referring to memory. It's not really necessary to know how it's implemented yet (in my opinion). Just know that it works.

The stack can also be referring to a type of container. You push data onto the top, and to get stuff out you pop it off the top. It's Last In First Out.

*** Excerpt from "The Doctrine of Programming, According To wasted_druid"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!