Suggested maximum size for stack distribution

Assuming there is a need for a buffer with a fixed size, is there a limit or threshold for the size, so with this size limit it’s enough to use the fast dedicated stack std::array , and above it is better to use std::vector with dynamic memory allocation from the heap ( since stack memory is precious and should not be consumed much)?

 // I think allocating 32 bytes on the stack is just fine. std::array<BYTE, 32> smallBuffer; // For 32KB, it better getting memory from the heap. std::vector<BYTE> bigBuffer(32*1024); // Is it better to allocate a 1KB buffer on the stack (with std::array) // or is it too much, and it better to allocate on the heap (using std::vector)? // What about 512 bytes? And 4KB? // Is there a suggested size threshold? std::array<BYTE, 1024> buffer; 
+6
source share
1 answer

There is no official limit. You can increase or decrease the default stack size for each system.

The default warning for the 16K stack is for the Visual Studio user mode application and 1K in kernel mode. Some static analyzer tools use the same warning limit.

 warning C6262: Function uses '30000' bytes of stack: exceeds /analyze:stacksize'16384'. Consider moving some data to heap 

http://msdn.microsoft.com/en-us/library/7yhee2f0.aspx

This is just a warning, but it can be considered as the recommended stack limit.

+2
source

All Articles