Article may be outdated

This article is 18 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
ibobev
How do functions like alloca allocate memory from the stack?
AI Summary

This article explains how the C function alloca allocates memory on the stack by utilizing the _chkstk function. It provides a technical breakdown of the assembly code generated on x86-64 architectures to ensure stack safety.

Why it matters

Understanding low-level memory management is critical for systems programming and preventing stack overflow vulnerabilities in high-performance software.

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