Example: realloc

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