Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you worry about stack size?

Tags:

c++

When you are programming in a language that allows you to use automatic allocation for very large objects, when and how do you worry about stack size? Are there any rules of thumb for reasoning about stack size?

like image 754
Tarquila Avatar asked Dec 16 '09 16:12

Tarquila


People also ask

How big is too big for the stack?

Stack overflow The stack has a limited size, and consequently can only hold a limited amount of information. On Windows, the default stack size is 1MB. On some unix machines, it can be as large as 8MB. If the program tries to put too much information on the stack, stack overflow will result.

How big should a stack be?

Stacks are temporary memory address spaces used to hold arguments and automatic variables during invocation of a subprogram or function reference. In general, the default main stack size is 8 megabytes.

What determines the size of the stack?

FWIW, stack size is not fixed, it expands at runtime as necessary, but stack limit is fixed. If your program only uses 40 bytes of stack (plus a little more for the run-time system) and the OS by default allocates an 8MB stack for the process (like Linux does), you still have almost 8MB stack left over.

Why is the stack size limited?

Because all threads in a process share the same address space, they have to divide it between them. And after the operating system has taken its part, there is "only" 2-3 GB left for an application. And that size is the limit for both the physical and the virtual memory, because there just aren't any more addresses.


2 Answers

When you are programming in a language that allows you to use automatic allocation for very large objects ...

If I want to allocate a very large object, then instead of on the stack I might allocate it on the heap but wrapped in an auto_ptr (in which case it will be deallocated when it goes out of scope, just like a stack-resident object, but without worrying about stack size).

... when and how do you worry about stack size?

I use the stack conservatively out of habit (e.g. any object bigger than about 512 bytes is allocated on the heap instead), and I know how big the stack is (e.g. about a megabyte by default), and therefore know that I don't need to worry about it.

Are there any rules of thumb for reasoning about stack size?

  • Very big objects can blow the stack
  • Very deep recursion can blow the stack
  • The default stack size might be too big (take too much total memory) if there are many threads and if you're running on a limited-memory embedded device, in which case you might want to use an O/S API or linker option to reduce the size of the stack per thread.
like image 72
ChrisW Avatar answered Sep 21 '22 15:09

ChrisW


You care about it on a microcontroller, where you often have to specify stack space explicitly (or you get whatever's left over after RAM gets used for static allocation + any RAM program space).

like image 44
Jason S Avatar answered Sep 24 '22 15:09

Jason S