Allocate memory


#include <stdlib.h>

void* malloc( size_t size );


The number of bytes to allocate.



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


The malloc() function allocates a buffer of size bytes. Use free() or realloc() to free the block of memory.

Note: Because the malloc() implementation uses signed, 32-bit integers to represent the size internally, you can't allocate more than 2 GB in a single allocation. If the size is greater than 2 GB, malloc() indicates an error of ENOMEM.

If size is zero, the default behavior is to return a non-NULL pointer that's valid only to a corresponding call to free() or realloc(). Don't assume that this pointer points to any valid memory. You can control this behavior via the MALLOC_OPTIONS environmental variable; if the value of MALLOC_OPTIONS contains a V, malloc() returns a NULL pointer. This environment variable also affects calloc() and realloc(). This is known as the “System V” behavior; for more information, see the entry for mallopt().

Note: If there isn't a free block of memory that's large enough to satisfy the request, the memory allocator uses mmap() to get memory from the system. The _amblksiz global variable (defined in <stdlib.h>) specifies the number of bytes that the allocator gets from the system. You can also use the mallopt() function to control how the system allocates memory.


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


Not enough memory.


#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;

Environment variables:

You can modify the allocator characteristics by calling mallopt() or by setting environment variables. You can control:

For more information, see the entry for mallopt() and Dynamic memory management in the Heap Analysis chapter of the Neutrino Programmer's Guide.


ANSI, POSIX 1003.1

Cancellation point No
Interrupt handler No
Signal handler No
Thread Yes


Don't use brk() and sbrk() with any other memory functions (such as malloc(), mmap(), and free()). The brk() function assumes that the heap is contiguous; in Neutrino, memory is returned to the system by the heap, causing the heap to become sparse. The Neutrino malloc() function is based on mmap(), and not on brk().

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

See also:

_amblksiz, calloc(), free(), mallopt(), mmap(), realloc()

Heap Analysis chapter of the Neutrino Programmer's Guide