5 Replies - 736 Views - Last Post: 20 April 2008 - 12:51 PM Rate Topic: -----

#1 DominationXVI  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 02-February 08

The Stack

Post icon  Posted 18 April 2008 - 10:39 PM

The term "stack" is thrown around quite a bit in the programming world, yet it is a concept that, even after a little research, still seems a bit ambiguous. So I have a couple of questions. I know that the stack is an area of memory the holds a function's arguments, variables, and return value. I understand how stuff is put on the stack (LIFO). What i'm a bit confused about is the size of the stack. Wouldn't this size ultimately just be limited by the amount of available ram on the system. If this is the case, why is stack overflow such a concern, especially with the amount of ram seen in today's computers? Also, right now on my computer, I am running a few different programs. Do all of these programs have there own "stack", or are they all just clumped into one big stack? Any help would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: The Stack

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,126
  • Joined: 18-April 07

Re: The Stack

Posted 18 April 2008 - 11:12 PM

To answer your first question about stack overflow, typically it is not a huge concern like it was in the days of old where memory is limited. Even today in mobile devices operating with limited RAM, it could still be a problem. You have to remember what is in RAM... other programs all eating up their share of RAM so if you are maxing out your RAM with open applications obviously you can hit stack overflows still. The main talk behind the stack overflow problem is not one of limited resources but more of a talk about should a program consume that much resources. You want the smallest "footprint" as possible so that there are more resources to spread around.

You never want to hear that your program is a resource hog and that is why the user can't run their other apps.

As for call stacks and sharing... each program runs their own call stack. Stacks are not mixed. When a program starts it allocates memory for itself and its execution stack which it has direct control over. This leads back to point number one... you don't want your program allocating all this space for its stack because there is less for everyone else. You don't want 10 apps all thinking they have unlimited resources because together they could easily eat up everything and get your stack overflows.

So in summary, as a programmer you want small stacks to provide the most resources for the user (on a collective basis not just on an individual program basis) and all programs maintain their own call stacks for execution.

Hope I was of help. :)
Was This Post Helpful? 0
  • +
  • -

#3 GravityGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-January 08

Re: The Stack

Posted 18 April 2008 - 11:26 PM

Memory is managed differently depending on the operating system, or the version of the OS. Each application these days is partitioned into its own memory space in case it crashes. As far as the user/programmer is concerned the memory is divided into two areas, the stack and the heap. The heap is the bulk of the memory space where you allocate memory to variables or objects typically with 'new'. The stack normally holds function return pointers, function parameters, local variables, structure data types, etc. It takes processor time to push and pop a lot of information to and from the stack so this is why you don't pass large arrays as value type parameters because they get copied on the stack. You pass them as refrence types and if you aren't changing the values of the array you can set them as constant references to constants but that's getting complicated.

You generally don't have to worry about stack space but if you have an abnormally large stack requirement I believe the compiler has a switch to increase the default size of the stack. When a function is called all local variables are allocated on the stack and exist as long as the function is active. Once program control exists the function and returns to the calling function, all the local variables are removed from the stack. A recursive function eats up stack space especially if the function is called many, many times. It all eventually has to unwind.

Once the program terminates, the entire stack space associated with the partition is returned to the heap as well. This insulates a single application from the OS.
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • Joined: 14-September 07

Re: The Stack

Posted 19 April 2008 - 01:24 AM

Embedded systems would be an area where you can't exceed the available RAM or your device will not work.
Was This Post Helpful? 0
  • +
  • -

#5 DominationXVI  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 74
  • Joined: 02-February 08

Re: The Stack

Posted 19 April 2008 - 01:57 PM

View PostMartyr2, on 18 Apr, 2008 - 11:12 PM, said:

To answer your first question about stack overflow, typically it is not a huge concern like it was in the days of old where memory is limited. Even today in mobile devices operating with limited RAM, it could still be a problem. You have to remember what is in RAM... other programs all eating up their share of RAM so if you are maxing out your RAM with open applications obviously you can hit stack overflows still. The main talk behind the stack overflow problem is not one of limited resources but more of a talk about should a program consume that much resources. You want the smallest "footprint" as possible so that there are more resources to spread around.

You never want to hear that your program is a resource hog and that is why the user can't run their other apps.

As for call stacks and sharing... each program runs their own call stack. Stacks are not mixed. When a program starts it allocates memory for itself and its execution stack which it has direct control over. This leads back to point number one... you don't want your program allocating all this space for its stack because there is less for everyone else. You don't want 10 apps all thinking they have unlimited resources because together they could easily eat up everything and get your stack overflows.

So in summary, as a programmer you want small stacks to provide the most resources for the user (on a collective basis not just on an individual program basis) and all programs maintain their own call stacks for execution.

Hope I was of help. :)



Thank you kindly for a concise and informative response. I'm always impressed by the quality of stuff posted on this forum; it's nice to know there are people out there who geniunely want to help others learn.
Was This Post Helpful? 0
  • +
  • -

#6 mikeblas  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 390
  • Joined: 08-February 08

Re: The Stack

Posted 20 April 2008 - 12:51 PM

I think it's an important distinction that every thread gets a stack, not every program.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1