Why don’t we allow stacks to be sparse, instead of forcing them to be contiguous?
cppdashboard.dev/r/2026/09/why-don-t-we-allow-stacks-to-be-sparse-instead-of-forcing-thThis article discusses the implications of allowing sparse stacks in programming, particularly focusing on security vulnerabilities and memory management issues that arise from non-contiguous stack allocation.
From the article
When I discussed why we don’t just make the entire stack out of guard pages , commenter BCS wondered , “Why require that the stack use contiguously mapped pages? What would break if only touched pages got mapped in? That could actually be a good thing for example with a function that wanted to alloca 512MB on the stack but only read/writes a few pages.”
So the question is asking why the stack must be contiguous. Why not let it be sparse and fault in only the pages that are touched?
The first issue is that the stack check code would have to include an explicit check against the stack limit, instead of just walking down the stack a page at a time. This explicit check is needed to avoid security vulnerabilities if somebody manages to alloca a buffer so large that it goes past the end of…
Share this resource