Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Windows does not commit the stack memory immediately; instead, it reserves the address space for it, and commits it page-by-page when it is accessed. Read <a href="http://msdn.microsoft.com/en-us/library/ms686774%28VS.85%29.aspx" rel="noreferrer">this page</a> for more info.</p> <p>As a result, stack address space consists of three contiguous regions:</p> <ul> <li>Reserved but uncommitted memory which can be used for stack growth (but was never accessed yet);</li> <li>Guard page, which was never accessed yet too, and serves to trigger stack growth when accessed;</li> <li>Committed memory, i.e. stack memory which was ever accessed by the thread.</li> </ul> <p>This allows us to construct a function that obtains stack size (with page size granularity):</p> <pre><code>static size_t GetStackUsage() { MEMORY_BASIC_INFORMATION mbi; VirtualQuery(&amp;mbi, &amp;mbi, sizeof(mbi)); // now mbi.AllocationBase = reserved stack memory base address VirtualQuery(mbi.AllocationBase, &amp;mbi, sizeof(mbi)); // now (mbi.BaseAddress, mbi.RegionSize) describe reserved (uncommitted) portion of the stack // skip it VirtualQuery((char*)mbi.BaseAddress + mbi.RegionSize, &amp;mbi, sizeof(mbi)); // now (mbi.BaseAddress, mbi.RegionSize) describe the guard page // skip it VirtualQuery((char*)mbi.BaseAddress + mbi.RegionSize, &amp;mbi, sizeof(mbi)); // now (mbi.BaseAddress, mbi.RegionSize) describe the committed (i.e. accessed) portion of the stack return mbi.RegionSize; } </code></pre> <p>One thing to consider: <code>CreateThread</code> allows to specify initial stack commit size (via <code>dwStackSize</code> parameter, when <code>STACK_SIZE_PARAM_IS_A_RESERVATION</code> flag is not set). If this parameter is nonzero, our function will return correct value only when stack usage becomes greater than <code>dwStackSize</code> value.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload