Go to main content

man pages section 3: Basic Library Functions

Exit Print View

Updated: Wednesday, July 27, 2022
 
 

alloca(3C)

Name

alloca - temporary memory allocator

Synopsis

#include <alloca.h>

void *alloca(size_t size);

Description

The alloca() function allocates size bytes of space in the stack frame of the caller, and returns a pointer to the allocated block. This temporary space is automatically freed when the caller returns or calls a stack frame altering function such as setjmp(3C). If the allocated block extends beyond the current stack limit, the resulting behavior is undefined.

Return Values

Upon successful completion, alloca() returns a pointer to space suitably aligned (after possible pointer coercion) for storage of any type of object.

alloca() may return a pointer to memory which causes a stack overflow if the full block is accessed.

Usage

Memory allocated by this function may be on pages with the PROT_EXEC access protections disabled to block execution of machine instructions, depending on the system settings for NXSTACK in sxadm(8) and any –z sx=nxstack options that were passed to ld(1) when linking the application. These settings may be overridden for individual pages by calling mprotect(2).

Attributes

See attributes(7) for descriptions of the following attributes:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Interface Stability
Committed
MT-Level
Safe

See Also

brk(2), getrlimit(2), mprotect(2), malloc(3C), pthread_attr_getstacksize(3C), strdupa(3C), attributes(7)

Warnings

The implementation of alloca() is both machine and compiler dependent; its use is discouraged.

Undefined results will occur if the size requested from a process's main thread for a block of memory exceeds the maximum size of a process's stack, which can be obtained with getrlimit(2); or if the size requested from another thread exceeds the maximum size of the thread stack, which can be obtained with pthread_attr_getstacksize(3C).