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 should not be used in an expression that is an argument to a function.

Returns:

The alloca() function returns a pointer to the start of the allocated memory. The return value is NULL if there is insufficient stack space available.

See also:

calloc(), malloc(), stackavail()

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

Systems:

MACRO