Realloc replaces an object even when the address stays the same
A successful `realloc` ends the old allocation’s lifetime and returns a pointer to a new object. Treating it as a possible move only misses the crucial rule: every old alias must be retired, even if the returned address compares as unchanged.
The return value describes a new lifetime
A dynamic array grows naturally with realloc: ask for a larger region and keep the existing elements. The call looks like a resize operation on one object, but C specifies a sharper model. On success, realloc deallocates the old allocated object and returns a pointer to a new object. It preserves the contents through the smaller of the old and new sizes. The result may have the same pointer value as the argument, but that is an address observation, not continuity of object identity. [1] [2]
This matters because C attaches validity to object lifetime, not merely to a numeric address. The standard says the value of a pointer becomes indeterminate when the object it points to, or just past, reaches the end of its lifetime. A successful call ends that lifetime. So an old pointer should not be reused, compared, logged, or passed onward after success. Assign the returned pointer into the owner and make sure other aliases do not survive the call. [1]
The familiar temporary pointer solves only the failure half
The common safe idiom saves the return value before replacing the owner: int *next = realloc(data, bytes);. If next is null, the old allocation remains unchanged and data is still the live owner. If next is non-null, success has ended the old object’s lifetime, so set data = next and discard every pointer derived from the old allocation. This avoids the classic leak from data = realloc(data, bytes): if allocation fails, the only pointer to the still-live allocation would be overwritten by null. [1]
The temporary does not make stale aliases safe. Imagine a vector that exposes int *item = &data[i] to a helper, or stores such pointers in another table. If the vector may be reallocated, those references need an explicit rule: they are borrowed only until the next capacity change, or they must be represented as indices and reacquired afterward. An index survives movement because it describes a position in the container; a raw interior pointer describes a location in one allocation’s lifetime.
Same-address success is not a portable exception
An allocator can sometimes extend a block in place. A debugger may then show the same hexadecimal address before and after the call. It is tempting to infer that old aliases still work because the bytes were not physically copied. That inference substitutes an implementation detail for the language contract. The returned pointer is the one that designates the new allocated object. Do not write code that asks whether the old and new pointer values compare equal: the old pointer has passed its object’s lifetime boundary. [1] [2]
A useful analogy is replacing a file by atomically renaming a new file over its pathname. The name can remain constant while the underlying object changes. Here the returned pointer is the refreshed handle. C makes the lifetime distinction especially important because optimizers may reason from the abstract machine rather than a particular allocator’s physical behavior.
Make ownership visible at the call site
A small ownership convention makes the rule practical. Keep one owning pointer for the allocation. Before a possible reallocation, do not retain element pointers across the call. After a successful return, replace the owner and reacquire any element addresses from it. On failure, keep the old owner and its contents. If another subsystem needs a long-lived reference, give it an index, a stable identifier, or a separately allocated object whose lifetime is independent of the resizable array.
Also keep the requested size meaningful. When a count comes from input, check that multiplying it by sizeof *data is representable before forming the byte size; otherwise the allocator may be asked for fewer bytes than the logical element count requires. Keep zero-size behavior out of the ordinary growth path: use free when the intent is to release storage, and handle an empty container explicitly. This separates the growth policy from edge cases in library and language versions.
A review checklist for resizable storage
When reviewing a realloc call, ask four concrete questions. Is the result first stored separately so failure preserves the owner? On success, is the owning pointer replaced before any later use? Can callbacks, iterators, cached element pointers, or another thread still hold an alias? Is the byte-size calculation checked before the call? These questions expose both the mechanical failure case and the more subtle lifetime case.
The key invariant is simple: after successful realloc, every pointer into the former allocation is retired, and only the returned pointer begins the next period of access. Thinking in terms of object lifetimes makes in-place growth and movement the same case. The allocator may choose where the new object lives; the program remains responsible for following the pointer and ownership contract.