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) and free(3) ๐Ÿ”ฅ
  • in certain cases, it can also simplify memory deallocation in applications that use longjmp(3) or siglongjmp(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 to free(3) space allocated by alloca()! ๐Ÿšจ 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms