realloc

Memory Allocation Functions

Reallocates (adjusts the size of) a block of memory.

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

mem_addressThe name of the block of memory whose size is changed.
sizeThe size in bytes of the block of memory requested.

Return Values

Returns a void * type pointer to the allocated space. If the system could not allocate the requested block of memory or if any of the parameters was 0, returns a NULL pointer.

For realloc details, refer to your C language documentation.

Example

In the following example, realloc enlarges a previously allocated buffer, buf, of length 1024, to a length of 2048 bytes.

#include <malloc.h>
    char * buf, * biggerbuf;
    if ((buf = (char *)malloc(1024, sizeof(char))) == NULL) {
        lr_output_message ("Insufficient memory available");
        return -1;
    }
    lr_output_message ("Memory allocated. Buffer address = %.8x", buf);
    // Now reallocate buf to size of 2048 bytes 
    if ((biggerbuf = (char *)realloc(buf, 2048)) == NULL) {
        lr_output_message ("Unable to re-allocate memory");
        return -1;
    }
    buf = biggerbuf;
    lr_output_message ("Memory reallocated. Buffer address = %.8x", buf);
    // Do something with the buffer here ...
    // Now free the buffer
    free(buf);
Example: Output:
Action.c(11): Memory allocated. Buffer address = 007b9d88
Action.c(20): Memory reallocated. Buffer address = 007b9d88