{"id":112617,"date":"2026-08-17T08:54:40","date_gmt":"2026-08-17T15:54:40","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112617"},"modified":"2026-08-17T08:54:40","modified_gmt":"2026-08-17T15:54:40","slug":"20260817-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260817-40\/?p=112617","title":{"rendered":"How do functions like <CODE>alloca<\/CODE> allocate memory from the stack?"},"content":{"rendered":"<p>A little while ago, I talked about <a title=\"How do compilers ensure that large stack allocations do not skip over the guard page?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260311-00\/?p=112134\"> how compilers ensure that large stack allocations do not skip over the guard page<\/a>. Shawn Van Ness was curious <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20260311-00\/?p=112134&amp;commentid=143923#comment-143923\"> how this works with <code>_alloca<\/code><\/a>. &#8220;Does it do the necessary <code>_chkstk()<\/code> probing?&#8221;<\/p>\n<p>Yes, the <code>_alloca()<\/code> function calls the same <code>_chkstk()<\/code> function to probe the stack before adjusting the stack pointer for the allocated memory.<\/p>\n<p>Here&#8217;s an artificial example:<\/p>\n<pre>#include &lt;malloc.h&gt;\r\n\r\nvoid consume(void*,void*);\r\n\r\nvoid f(int n)\r\n{\r\n    char buffer[16384];\r\n    consume(alloca(n), buffer);\r\n}\r\n<\/pre>\n<p>On x86-64, this results in<\/p>\n<pre>        push    rbp\r\n        mov     eax, 16416          ; probe for local frame\r\n        call    __chkstk\r\n        sub     rsp, rax            ; create local frame\r\n\r\n        lea     rbp, [rsp+32]\r\n\r\n        movsxd  rax, ecx            ; n\r\n        lea     rcx, [rax+15]       ; round up to multiple of 16\r\n        and     rcx, -16\r\n\r\n        mov     rax, rcx            ; special __chkstk calling convention\r\n        call    __chkstk\r\n        sub     rsp, rcx            ; allocate n bytes\r\n\r\n        lea     rdx, [rbp]          ; rdx -&gt; buffer\r\n        lea     rcx, [rsp+32]       ; rcx -&gt; alloca'd memory\r\n        call    consume\r\n\r\n        lea     rsp, [rbp+16384]    ; clean up local frame\r\n        pop     rbp\r\n        ret     0\r\n<\/pre>\n<p>Observe that the same <code>__chkstk<\/code> function is used both for performing the initial stack probe when creating the local frame as well as for the <code>alloca()<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The usual probing.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-112617","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The usual probing.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=112617"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112617\/revisions"}],"predecessor-version":[{"id":112618,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112617\/revisions\/112618"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=112617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}