Borland C++ reference

alloca

raw OCR

Allocates temporary stack space.

Defined in header <malloc.h>

Syntax

#include <malloc.h>
void *alloca(size_t size);

Portability

DOSUNIXWindowsANSI CC++ only
■■■

Remarks

alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.

Because alloca modifies the stack pointer, do not place calls to alloca in an expression that is an argument to a function.

If the calling function does not contain any references to local variables in the stack, the stack will not be restored correctly when the function exits, resulting in a program crash. To ensure that the stack is restored correctly, use the following code in the calling function: char *p; char dummy[1];

dummy[0] = 0;

p = alloca(nbytes);

Return value

If enough stack space is available, alloca returns a pointer to the allocated stack area. Otherwise, it returns NULL.

See also

Example

#include <stdio.h>
#include <malloc.h>
void test (int a)
{
  char *newstack;
  int len = a;
  char dummy [1] ;
  dummy [0] = 0;          n  force good stack frame */
  printfC'SP before  calling alloca(Ox%X    Ox%X\n",len,_SP)
  newstack =  (char *) alloca(len);
  printfC'SP after  calling alloca = Ox%X\n",_SP);
  if  (newstack)
    printf ("Alloca(Ox%X) returned %p\n",len,newstack);
  else
    printf ("Alloca(Ox%X) failed\n",len);

void mainO
{
  test (256),•
  test (16384),•

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.