Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer


Allocates memory on the stack.

void *_alloca( 
   size_t size 


Bytes to be allocated from stack.

Return Value

The _alloca routine returns a void pointer to the allocated space, which is guaranteed to be suitably aligned for storage of any type of object. A stack overflow exception is generated if the space cannot be allocated.


_alloca allocates size bytes from the program stack. The allocated space is automatically freed when the calling function exits (not when the allocation merely passes out of scope). Therefore, do not pass the pointer value returned by _alloca as an argument to free.

There are restrictions to explicitly calling _alloca in an exception handler (EH). EH routines that run on x86-class processors operate in their own memory frame: They perform their tasks in memory space that is not based on the current location of the stack pointer of the enclosing function. The most common implementations include Windows NT structured exception handling (SEH) and C++ catch clause expressions. Therefore, explicitly calling _alloca in any of the following scenarios results in program failure during the return to the calling EH routine:

  • Windows NT SEH exception filter expression:   __except (_alloca () )
  • Windows NT SEH final exception handler:   __finally {_alloca () }
  • C++ EH catch clause expression

However, _alloca can be called directly from within an EH routine or from an application-supplied callback that gets invoked by one of the EH scenarios previously listed.

Security Note   In Windows XP, if _alloca is called inside a try/catch block, you must call _resetstkoflw in the catch block.


Routine Required header Compatibility
_alloca <malloc.h> Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_alloca.c
#include <windows.h>
#include <stdio.h>

int main() {
   __try {
      void *p = _alloca(0xffffff);
   __except( GetExceptionCode() == STATUS_STACK_OVERFLOW ) {
      printf ("_alloca failed!\n");


_alloca failed!

See Also

Memory Allocation Routines | calloc | malloc | realloc | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft