reallocarray()

QNX SDP8.0C Library ReferenceAPIDeveloper

Allocate or reallocate a block of memory occupied by an array

Synopsis:

#include <stdlib.h>

void* reallocarray( void *ptr,
                    size_t nmemb,
                    size_t size );

Arguments:

ptr
A pointer to the block of memory occupied by the array to be reallocated. This can be NULL if you're allocating and not reallocating the memory; see below for details.
nmemb
The number of elements in the new array.
size
The size, in bytes, of one element of the new array.

Library:

libc

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

Description:

The reallocarray() function allocates or reallocates the block of memory occupied by an array specified by ptr based on the rules listed below. It also checks the "new size" (nmemb*size) for overflow.

  • If the total "new size" is zero (which means size and/or nmemb is 0), the function returns a non-NULL pointer that's valid only to a corresponding call to free(), realloc(), reallocarray() or recallocarray(). Don't assume that this pointer points to any valid memory.
  • If the total "new size" is not zero (which means size and nmemb are not 0) and ptr is NULL, the function allocates a new block of cleared memory of size nmemb * size (which is checked for overflow).
  • Otherwise, when ptr is non-NULL and the total "new size" is not zero (which means size and nmemb are not 0), reallocarray() reallocates space for an array with nmemb elements of size bytes each by:
    • shrinking the size of the allocated memory block ptr when size times nmemb is smaller than the current size of ptr
    • extending the allocated size of the memory block ptr if there is a large enough block of unallocated memory immediately following ptr
    • allocating a new block with the appropriate size (size * nmemb), and copying the contents of ptr to this new block

The reallocarray() function allocates memory from the heap. Use free() to free the block of memory.

Note:
Because a new block could be allocated, any pointers into the old memory could be invalidated. These pointers will point to freed memory, with possibly bad results, when a new block is allocated.

The function returns NULL when the memory pointed to by ptr can't be reallocated. In this case, the memory isn't freed, so be careful to maintain a pointer to the old memory block so you can free it later.

In the following example, buffer is set to NULL if the function fails, and won't point to the old memory block. If buffer is your only pointer to the memory block, then you have lost access to this memory.

buffer = reallocarray( buffer, 100, sizeof(int) );

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;
    char* new_buffer;

    buffer = malloc( 80*sizeof(int) );
    if ( buffer == NULL ) {
        return EXIT_FAILURE;
    }

    new_buffer = reallocarray( buffer, 100, sizeof(int) );
    if( new_buffer == NULL ) {

        /* Couldn't allocate a larger buffer.
           Remember that buffer stills point to
           allocated memory -- don't leak it! */

        free(buffer);
        return EXIT_FAILURE;
    } else {
        buffer = new_buffer;
    }

    return EXIT_SUCCESS;
}

Environment variables:

See the entry for mallopt().

Classification:

BSD

Safety:
Cancellation pointNo
Signal handlerNo
ThreadYes
Page updated: