Changes the size of an allocated block of heap memory.
#include <stdlib.h> void *_realloc(void *ptr, size_t size);
ptr
A pointer to an allocated block of memory.
size
The new size to allocate.
The _ realloc() function changes the size of the memory block pointed to by ptr to size bytes. The size argument can have a value smaller or larger than the current size of the block ptr points to. The ptr argument should be a value assigned by the memory allocation functions _ calloc() and _ malloc().
If size is 0, the memory block pointed to by ptr is released. If ptr is a null pointer, _ realloc() allocates size bytes.
The old contents of the memory block are preserved in the new block if the new block is larger than the old. If the new block is smaller, the extra bytes are cut from the end of the old block.
_realloc() returns a pointer to the new block if it is successful and size is greater than 0. _ realloc() returns a null pointer if it fails or size is 0.
#include <stdlib.h> int main(void { int* vec; int* newvec int count; count = 3; vec = _calloc(count, sizeof(int)); if (vec == NULL) { printf("Could not allocate.\n"); exit(1); } vec[0] = 32; vec[1] = 39; vec[2] = 41; /* Assign _realloc()'s result to newvec to preserve vec's value if the call to _realloc() fails. */ count = 5; newvec = _realloc(vec, count * sizeof(int)); if (newvec != NULL) { vec = newvec; vec[3] = 58; vec[4] = 82; } return 0; }