Warning: main(/www/www/htdocs/style/globals.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/a/alloca.html on line 1

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/globals.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/a/alloca.html on line 1

Warning: main(/www/www/htdocs/style/header.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/a/alloca.html on line 8

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/header.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/a/alloca.html on line 8

alloca()

Allocate automatic space from the stack

Synopsis:

#include <alloca.h>

void* alloca( size_t size );

Arguments:

size
The number of bytes of memory to allocate.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

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.


Note: Don't use this function in an expression that's an argument to a function.

Returns:

A pointer to the start of the allocated memory, or NULL if an error occurred (errno is set).

Examples:

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

FILE *open_err_file( char *name )
{
    char *buffer;

    /* allocate temporary buffer for file name */
    buffer = (char *)alloca( strlen( name ) + 5 );

    if( buffer ) {
        FILE *fp;

        sprintf( buffer, "%s.err", name );
        fp = fopen( buffer, "w" );

        return fp;
    }

    return (FILE *)NULL;
}

int main( void )
{
    FILE *fp;

    fp = open_err_file( "alloca_test" );
    if( fp == NULL ) {
        printf( "Unable to open error file\n" );
    } else {
        fprintf( fp, "Hello from the alloca test.\n" );
        fclose( fp );
    }

    return EXIT_SUCCESS;
}

Classification:

Unix

Safety:
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes

Caveats:

Don't use alloca() as an argument to a function.

See also:

calloc(), malloc()


Warning: main(/www/www/htdocs/style/footer.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/a/alloca.html on line 170

Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/footer.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/a/alloca.html on line 170