What is reasonable indicative size when it is better to go from stack to heap?

c, c++, memory-management

Solution

You might want to check your system's default stack size, and consider whatever use your application makes of recursion, to arrive at some reasonable threshold.

Anyway, for typical desktop PCs I'd say ~100kb was reasonable to put on the stack for function that won't be invoked recursively without any unusual considerations (I had to revise that downwards after seeing how restrictive Windows was below). You may be able to go an order of magnitude more or less on specific systems but it's around that point you'd start to care about checking your system limits.

If you find you're doing that in many functions, you'd better think carefully about whether those functions could be called from each other, or just allocate dynamically (preferably implicitly via use of `vector`, `string` etc.) and not worry about it.

The 100kb guideline is based on these default stack size numbers ripped from the 'net:

platform    default size    # bits  # digits
    ===============================================================
SunOS/Solaris   8172K bytes <=39875 <=12003 (Shared Version)
Linux       8172K bytes <=62407 <=18786
Windows     1024K bytes <=10581 <=3185  (Release Version)
cygwin      2048K bytes <=3630  <=1092

Problem

Is ``` WCHAR bitmapPathBuffer[512] ``` ok for stack allocation? or it is better to use heap for this size? What is reasonable indicative size when it is better to go from stack to heap... all says "is depens" but our brains need some limits to orients.

Original source

Related problems