OS/Compiler Stack Limits: Defaults Vary Widely

Common default stack sizes range from 8MB on Linux/macOS to 1MB or less on Windows and some embedded systems, often configurable via compiler or OS settings.

The top 3

  1. Default Stack Sizes: Windows, Linux, macOS Comparison: Windows typically defaults to a 1MB stack size, while Linux systems commonly provide 8MB (per thread) and macOS often uses 8MB for the main thread and 512KB for created threads, though these can vary by compiler and build environment.
  2. Modifying Stack Limits: Compiler Flags & Runtime APIs: Developers can adjust stack limits using `ulimit -s` on Linux/macOS for the current shell and its children, the `/STACK` linker option or `EDITBIN` utility on Windows, and `SetThreadStackSize` API for dynamic thread stack allocation.
  3. Stack Overflow Triggers: Deep Recursion & Large Locals: The most common causes of stack overflow errors are excessively deep or infinite recursion, where a function repeatedly calls itself without a proper base case, and allocating very large arrays or objects on the stack as local variables.

Sources

Open the full topic