Often you do not know for certain how big a block you will ultimately need at the time you must begin to use the block. For example, the block might be a buffer that you use to hold a line being read from a file; no matter how long you make the buffer initially, you may encounter a line that is longer.
You can make the block longer by calling realloc or
reallocarray. These functions are declared in stdlib.h.
Preliminary: | MT-Safe | AS-Unsafe lock | AC-Unsafe lock fd mem | See POSIX Safety Concepts.
The
reallocfunction changes the size of the block whose address is ptr to be newsize.Since the space after the end of the block may be in use,
reallocmay find it necessary to copy the block to a new address where more free space is available. The value ofreallocis the new address of the block. If the block needs to be moved,realloccopies the old contents.If you pass a null pointer for ptr,
reallocbehaves just like ‘malloc (newsize)’. This can be convenient, but beware that older implementations (before ISO C) may not support this behavior, and will probably crash whenreallocis passed a null pointer.
Preliminary: | MT-Safe | AS-Unsafe lock | AC-Unsafe lock fd mem | See POSIX Safety Concepts.
The
reallocarrayfunction changes the size of the block whose address is ptr to be long enough to contain a vector of nmemb elements, each of size size. It is equivalent to ‘realloc (ptr, nmemb * size)’, except thatreallocarrayfails safely if the multiplication overflows, by settingerrnotoENOMEM, returning a null pointer, and leaving the original block unchanged.
reallocarrayshould be used instead ofreallocwhen the new size of the allocated block is the result of a multiplication that might overflow.Portability Note: This function is not part of any standard. It was first introduced in OpenBSD 5.6.
Like malloc, realloc and reallocarray may return a null
pointer if no memory space is available to make the block bigger. When this
happens, the original block is untouched; it has not been modified or
relocated.
In most cases it makes no difference what happens to the original block
when realloc fails, because the application program cannot continue
when it is out of memory, and the only thing to do is to give a fatal error
message. Often it is convenient to write and use a subroutine,
conventionally called xrealloc, that takes care of the error message
as xmalloc does for malloc:
void *
xrealloc (void *ptr, size_t size)
{
void *value = realloc (ptr, size);
if (value == 0)
fatal ("Virtual memory exhausted");
return value;
}
You can also use realloc or reallocarray to make a block
smaller. The reason you would do this is to avoid tying up a lot of memory
space when only a little is needed.
In several allocation implementations, making a block smaller sometimes
necessitates copying it, so it can fail if no other space is available.
If the new size you specify is the same as the old size, realloc and
reallocarray are guaranteed to change nothing and return the same
address that you gave.