malloc()

QNX SDP8.0C Library ReferenceAPIDeveloper

Allocate memory

Synopsis:

#include <stdlib.h>

void* malloc( size_t size );

Arguments:

size
The number of bytes to allocate.

Library:

libc

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

Description:

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

If size is zero, then malloc() returns a valid pointer with a size of zero, which can be passed to a corresponding call to free() or realloc(). You cannot assume that this pointer points to any valid memory.

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. You can also use the mallopt() function to control how the system allocates memory.

The heap is protected by such security measures as address space layout randomization (ASLR), which randomizes the stack start address and code locations in executables and libraries, and heap cookies.

Returns:

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

Errors:

ENOMEM
Not enough memory.

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, POSIX 1003.1

Safety:
Cancellation pointNo
Signal handlerNo
ThreadYes
Page updated: