4

I mainly program with C++. I have seen from many places that I should put large objects (like array of 10k elements) on heap (using new) but not on stack (using pure array type). I don't really understand. The reason might be because abuse of stack many result in stack overflow error at runtime. But why should OS set a limit for the stack size of a process (or more precisely, a thread) as virtual memory can go as large as needed (or 4G in practice). Can anyone help me, I really have no clue.

Zhongqi Cheng
  • 283
  • 1
  • 5
  • There isn't a general limit. Check out this question: https://stackoverflow.com/questions/1825964 Why would you want a huge stack size anyway, you aren't supposed to need that. Recursion isn't so commonly used, especially with huge amounts of data stored on the stack. – BullyWiiPlaza Jun 12 '20 at 20:25
  • ***But why should OS set a limit for the stack size of a process*** In 32 bit applications the amount of stack space limited the # of threads you could create on windows. WIth 1MB stack you could create around 2000 total threads before you exhausted the whole 2GB of user address space. – drescherjm Jun 12 '20 at 20:26
  • Does this answer your question? [C/C++ maximum stack size of program](https://stackoverflow.com/questions/1825964/c-c-maximum-stack-size-of-program) – Waqar Jun 12 '20 at 20:26
  • ***(using new)*** Using `new` is discouraged in modern `c++` code. std::vector is more appropriate. – drescherjm Jun 12 '20 at 20:31
  • A re-sizable stack would be incredibly hard to to implement. Either it would be non-contiguous or pointer invalidation would be crippling. Here's a small stack (snerk snerk snerk) of different reasons: [why is stack memory size so limited?](https://stackoverflow.com/questions/10482974/why-is-stack-memory-size-so-limited) – user4581301 Jun 12 '20 at 20:32
  • 1
    This is a good question. But it has been asked before; do read the answers in the duplicate. – Bathsheba Jun 12 '20 at 20:35
  • On my platform, the stack is 1 MB (which is configurable), and the heap is 256 GB. There's more room on the heap. – Eljay Jun 12 '20 at 23:32

2 Answers2

4

Tradition, and threads.

Stacks are per thread, and for efficiency must be contiguous memory space. Non contiguous stacks make every function call more expensive.

Heaps are usually shared between threads; when they aren't, they don't have to be contiguous.

In the 32 bit days, having 1000 threads isn't impossible. At 1 meg per thread, that is 1 gigabyte of address space. And 1 meg isn't that big.

In comparison, 2 gigs of heap serves all threads.

On 64 bit systems, often addressable memory is way less than 64 bits. At 40 bits, if you gave half of the address space to stacks and you had 10,000 threads, that is a mere 50 megs per stack.

48 bits is more common, but that still leaves you with mere gigabytes of address space per stack.

In comparison, the heap has tebibytes.

What more is that with a large object on the stack doesn't help much with cache coherance; no cpu cache can hold the tront and the back. Having to follow a single pointer is trivial if you are working heavily with it, and can even ensure the stack stays in cache better.

So (a) stack size cost scales with threads (b) address space can be limited (c) the benefits of mega stacks are small.

Finally, infinite recursion is a common bug. You want your stack to blow and trap (the binary loader surrounds the stack with trap pages often) before your user shell crashes from resource exhastion. A modest size stack makes that more likely.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
-2

It's totally possible to use a static array of 10k INTs. It's usually better to use "new" because when you're dealing with big chunks of data, you can't guarantee they'll always be the size of your array. Dynamic allocation of memory lets you use only what you need. It's more efficient.

If your curious about how the OS chooses stack sizes or "pages", read this: https://en.m.wikipedia.org/wiki/Page_(computer_memory)

Also, I'm not sure you're using "heap" the right way. "heap" is a tree-based data structure. The Stack refers to the layout of the executable and process data in RAM.