[Previous]
[Contents]
[Next]

alloca()

allocate automatic space from the stack

Synopsis:

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

Description:

The alloca() function allocates space for an object of size bytes from the stack. The allocated space is automatically discarded when the current function exits. The alloca() function shouldn't be used in an expression that's an argument to a function.

Returns:

A pointer to the start of the allocated memory, or NULL if there isn't sufficient stack space available.

Examples:

#include <stdio.h>
#include <string.h>
#include <malloc.h>
FILE *open_err_file( char * );

void main()
  {
    FILE *fp;

    fp = open_err_file( "alloca" );
    if( fp == NULL ) {
      printf( "Unable to open error file\n" );
    } else {
      fclose( fp );
    }
  }

FILE *open_err_file( char *name )
  {
     char *buffer;
     /* allocate temp buffer for file name */
     buffer = (char *) alloca( strlen(name) + 5 );
     if( buffer ) {
       sprintf( buffer, "%s.err", name );
       return( fopen( buffer, "w" ) );
     }
     return( (FILE *) NULL );
  }

Classification:

WATCOM

Safety:
Interrupt handler No
Signal handler Yes
Thread Yes

Caveats:

alloca() is a macro.

See also:

calloc(), malloc(), stackavail()


[Previous]
[Contents]
[Next]