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/r/realloc.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/r/realloc.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/r/realloc.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/r/realloc.html on line 8

realloc()

Allocate, reallocate, or free a block of memory

Synopsis:

#include <stdlib.h>

void* realloc( void* old_blk, 
               size_t size );

Arguments:

old_blk
A pointer to the block of memory to be allocated, reallocated, or freed.
size
The new size, in bytes, for the block of memory.

Library:

libc

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

Description:

The realloc() function allocates, reallocates, or frees the block of memory specified by old_blk based on the following rules. You can control parts of this behavior via the MALLOC_OPTIONS environmental variable.

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


Note: Because it's possible that a new block will be allocated, any pointers into the old memory could be invalidated. These pointers will point to freed memory, with possible disastrous results, when a new block is allocated.

The realloc() function returns NULL when the memory pointed to by old_blk can't be reallocated. In this case, the memory pointed to by old_blk 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 = (char* )realloc( buffer, 100 );

Returns:

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

Errors:

ENOMEM
Not enough memory.
EOK
No error.

Examples:

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

int main( void )
{
    char* buffer;
    char* new_buffer;

    buffer = (char* )malloc( 80 );
    new_buffer = (char* )realloc( buffer, 100 );
    if( new_buffer == NULL ) {
        /* not able to allocate larger buffer */

        return EXIT_FAILURE;
    } else {
      buffer = new_buffer;
    }


    return EXIT_SUCCESS;
}

Environment variables:

MALLOC_OPTIONS
Control the way calloc(), malloc(), and realloc() behave if you specify a size of 0 (or a value of 0 for the n argument to calloc()). The V (“System V”) and R (“use the realloc() behavior of QNX Neutrino 6.4.0 and earlier”) columns below indicate how the functions behave if the value of MALLOC_OPTIONS includes that letter:
Function Default V R
calloc(n, 0) Non-NULL NULL No effect
malloc(0) Non-NULL NULL No effect
realloc(NULL, 0) Non-NULL NULL No effect
realloc(non-NULL, 0) Non-NULL NULL NULL

In all the above cases, if the function returns a non-NULL pointer, it's valid only for a corresponding call to free() or realloc().

Classification:

ANSI, POSIX 1003.1

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

See also:

calloc(), free(), malloc(), sbrk()


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/r/realloc.html on line 358

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/r/realloc.html on line 358