[Previous] [Contents] [Next]

malloc()

Allocate memory

Synopsis:

#include <malloc.h>

void* malloc( size_t size );

Library:

libc

Description:

The malloc() function allocates a buffer of size bytes.


Note: This function allocates memory in blocks of _amblksiz bytes (a global variable defined in <stdlib.h>).

Returns:

A pointer to the start of the allocated memory, or NULL if there's insufficient memory available.

Examples:

#include <stdlib.h>

int main( void )
{
    char* buffer;

    buffer = (char* )malloc( 80 );
    if( buffer != NULL ) {
        /* do something with the buffer */
        ...

        free( buffer );
    }
    
    return EXIT_SUCCESS;
}

Classification:

ANSI

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

Caveats:

In QNX 4, nothing is allocated when you malloc() 0 bytes. Be careful if your code is ported between QNX 4 and QNX 6.

See also:

calloc(), free(), realloc(), sbrk()


[Previous] [Contents] [Next]