October 2004
Beginner
408 pages
9h 24m
English
Sometimes you have to resize a block of dynamic memory after you have already stored data in it. The most common reason for this is the need to grow the block beyond the initial size given to the malloc() call. This often happens if the amount of data to be stored and processed is not known in advance (for example, when you are receiving data over a network connection).
The C library provides the realloc() function for this purpose. It takes two parameters: the address of the memory block to be resized and the desired new size of that block.
void *x, *y; x = malloc(1000); // Initial block y = realloc(x, 2000); // Doubled
Retaining the Original AddressSince realloc() can return a NULL pointer if the operation fails, ... |