Article may be outdated

This article is 46 days old. Some details may have changed since publication.

Hacker News·3 min read·hard

How do functions like alloca allocate memory from the stack?

I
ingve
How do functions like alloca allocate memory from the stack?
✦AI Summary

This article explains the technical mechanism behind how the alloca function allocates memory on the stack in C. It details the use of the __chkstk function to ensure stack safety and prevent skipping over guard pages.

Why it matters

Understanding low-level memory management is critical for systems programming and preventing security vulnerabilities like stack overflows.

✦Dive DeeperCreate a free account to unlock

A little while ago, I talked about how compilers ensure that large stack allocations do not skip over the guard page . Shawn Van Ness was curious how this works with _alloca . “Does it do the necessary _chkstk() probing?”

Yes, the _alloca() function calls the same _chkstk() function to probe the stack before adjusting the stack pointer for the allocated memory.

#include <malloc.h> void consume(void*,void*); void f(int n) { char buffer[16384]; consume(alloca(n), buffer); } On x86-64, this results in

Continue reading on Headlinne

Create a free account to read the full article.

Read full article →
technologyscience
✦

Get smarter about the news

Sign up free for a feed built around what you actually care about, Dive Deeper research on any story, and the full text of every article.

Create free account

Already have an account? Sign in