alloca(3)
โจ alloca(3) โจ โ the library functions manual page, but make it a group chat
okay bestie, gather round, because today we're talking about alloca() from the Library Functions Manual, and she's a little unhinged but honestly so iconic for it ๐
๐ธ NAME ๐ธ
alloca โ allocate memory that is automatically freed โจ
like, you ask for memory, you get memory, and then it just... evaporates when you leave. no free() needed. no cap.
๐ LIBRARY ๐
Standard C library (libc, -lc) ๐ซถ
๐ SYNOPSIS ๐
#include <alloca.h>
void *alloca(size_t size);
๐ DESCRIPTION ๐
so alloca() allocates size bytes of space in the stack frame of the caller. it's not heap memory, it's stack memory, which means it lives right there in your function's little house ๐
and this temporary space? it's automatically freed the moment the function that called alloca() returns to its caller. she cleans up after herself. main character energy fr fr ๐
๐ฆ RETURN VALUE ๐ฆ
alloca() returns a pointer to the beginning of the allocated space. easy peasy.
but heads up โ if the allocation causes stack overflow, program behavior is undefined. so like... don't do that ๐ญ
โญ ATTRIBUTES โญ
want the deets on what these terms even mean? go read attributes(7) bestie.
- Interface:
alloca()- Attribute: Thread safety
- Value:
MT-Safe๐
(translation: she's thread-safe. slay.)
๐ง STANDARDS ๐ง
None. literally none. she's ungoverned ๐
๐ HISTORY ๐
PWB, 32V. ancient. vintage. still here. iconic behavior honestly.
๐ NOTES ๐
okay so this is the part where it gets real.
alloca()is machine- and compiler-dependent. so like, she's giving โจadaptableโจ but also โจchaoticโจ.- because it allocates from the stack, it's faster than
malloc(3)andfree(3)๐ฅ - in certain cases, it can also simplify memory deallocation in applications that use
longjmp(3)orsiglongjmp(3). - otherwise? its use is discouraged. yeah. sorry babe.
because the space allocated by alloca() is allocated within the stack frame, that space is automatically freed if the function return is jumped over</> by a call to longjmp(3) or siglongjmp(3).
also important โ the space allocated by alloca() is not automatically deallocated if the pointer that refers to it simply goes out of scope. it is automatically deallocated when the caller function returns. so scope is not the vibe check here, the return is.
๐จ Do not attempt tofree(3)space allocated byalloca()! ๐จ like, seriously, don't. that's not her lane.
by necessity, alloca() is a compiler built-in, also known as __builtin_alloca(). by default, modern compilers automatically translate all uses of alloca() into the built-in, but this is forbidden if standards conformance is requested (-ansi, -std=c*), in which case <alloca.h> is required, lest a symbol dependency be emitted.
and because alloca() is a built-in, it is impossible to take its address or to change its behavior by linking with a different library. she is who she is. no rebranding ๐
โโ๏ธ
now, Variable length arrays (VLAs) โ they're part of the C99 standard, optional since C11, and can be used for a similar purpose. cute! but they do not port to standard C++, and, being variables, they live in their block scope and don't have an allocator-like interface, which makes them unfit for implementing functionality like strdupa(3). so like, close but no cigar ๐
๐ BUGS ๐
strap in, bestie, this is the messy part.
alloca() does not query the system for available stack memory, and does not fall back to using heap if stack storage is unavailable. so it therefore cannot indicate an error if the allocation fails. and if the allocation fails? the program is likely to receive a SIGSEGV signal ๐ญ๐ like... no graceful error handling, just vibes and then death.
also, on many systems alloca() cannot be used inside the list of arguments of a function call, because the stack space reserved by alloca() would appear on the stack in the middle of the space for the function arguments. messy. do not attempt.
๐ SEE ALSO ๐
and that's the whole page, bestie ๐ this was Linux man-pages 6.20, dated 2025-10-29, and if you ever need her again she's filed under alloca(3) ๐๐ฆโจ mwah, go write some stack code (carefully) ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: BSD-4-Clause-UC
- Copyright 1980-1991, Regents of the University of California
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms