C / Intermediate: Memory and Reliable Components / Lesson 14
Resizable arrays and ownership
Preserve C allocation ownership while growing a dynamic array.
On this page
Lesson goals
- Guard allocation size calculations.
- Preserve ownership across realloc success and failure.
- Maintain resizable-array length, capacity, and ownership invariants.
- Check growth arithmetic and preserve the original owner on nonzero realloc failure.
Define the capacity contract
Growing a dynamic array changes its capacity while preserving the logical number of elements already stored. Maintain the invariant that length never exceeds capacity, and distinguish capacity in elements from allocation size in bytes. Decide who owns the buffer and whether callers may keep pointers into it. A resize can move storage, so successful growth can invalidate pointers into the old allocation.
Check size and failure
Before requesting bytes, check that capacity multiplied by element size is representable as size_t. Also impose a practical maximum for input-controlled sizes. A successful arithmetic check does not guarantee enough memory is available. For a nonzero request, keep the existing pointer until realloc succeeds: overwriting it directly with a null failure result would lose the only reference to the old allocation.
#include <stdint.h>
#include <stdio.h>
int main(void) {
size_t count = 8;
size_t element_size = sizeof(int);
if (count > SIZE_MAX / element_size) return 1;
printf("%d\n", count * element_size / element_size == count);
return 0;
}
1
The multiplication follows a division guard; sizeof(int) is nonzero.
Keep ownership on failure
The example uses a temporary pointer for a nonzero growth request. On failure, the old allocation remains available and is freed by the error path. On success, the returned pointer becomes the sole owner used afterward, and the program frees it once. The values are fixed and small so this example illustrates ownership rather than claiming that allocation always succeeds.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int *values = malloc(2 * sizeof *values);
if (values == NULL) return 1;
values[0] = 7;
values[1] = 9;
int *grown = realloc(values, 4 * sizeof *values);
if (grown == NULL) {
free(values);
return 1;
}
values = grown;
printf("%d %d\n", values[0], values[1]);
free(values);
return 0;
}
7 9
The temporary preserves the old pointer if the nonzero request fails.
Refresh borrowed pointers
A successful realloc may return the same address or a different one. Do not use a saved pointer into the previous allocation after success; compute a new pointer from the returned base and a validated index. Store an index rather than an interior pointer across growth when the design permits it. Document whether an operation can resize so callers know when borrowed views expire.
Test growth and limits
Test an initially empty logical sequence, a growth preserving existing values, and a rejected oversized request. Exercise allocation failure through an injectable allocator or suitable test harness rather than assuming a huge request will always fail. Handle zero capacity explicitly in the application contract instead of relying on the implementation-specific details of a zero-size realloc. Run memory diagnostics on both success and failure paths.
Define the array’s ownership invariants
A resizable array needs a clear state model: a base pointer, logical length, and capacity measured in elements. Maintain length <= capacity, and require live storage for the capacity whenever the representation says it exists. A common empty policy is base == NULL, length == 0, and capacity == 0, but the interface must state it rather than relying on the behavior of zero-size allocation. The owner is responsible for releasing the current base exactly once.
Appending when length is below capacity writes at the current length and then advances it. When the array is full, reserve enough capacity before writing. Do not change length or publish a new capacity until the required allocation succeeds. A failure contract can promise that base, length, capacity, and existing elements remain unchanged; test all of those observations rather than the status alone.
Check growth arithmetic before allocation
Capacity growth has two arithmetic boundaries: deciding the new element count and converting that count to bytes. Check addition or doubling before performing it, and reject a count greater than SIZE_MAX / sizeof *base before multiplication. A policy that doubles capacity must also choose an initial nonzero capacity and a maximum; do not let wraparound produce a smaller allocation. sizeof *base follows the element type, unlike sizeof base, which measures the pointer object.
The growth policy is an application decision. A fixed increment may be simple but can require more reallocations; geometric growth can reduce their frequency while retaining unused capacity. Measure the actual workload and preserve correctness before choosing. The examples use small fixed sizes and explicit bounds; they do not make timing claims or assume a specific pointer size.
Keep the owner across realloc failure
For a nonzero resize, store realloc’s result in a temporary pointer. On failure it returns NULL and the original allocation remains allocated; the original base is still needed for access and eventual free. Update the owner and capacity only after success. Assigning realloc directly to the sole owner can lose the allocation on failure. Define zero-capacity behavior separately rather than relying on realloc with size zero.
A successful realloc may move storage. Old pointers into the allocation must not be used afterward; save a validated element index instead and recompute an address from the returned base. Even if the address appears unchanged in a run, the safe contract uses the returned pointer. Newly added capacity is not initialized by realloc, so initialize elements before reading them. Existing bytes are preserved only within the applicable old and new sizes.
Separate owner, alias, and transfer
The base pointer is the owner used for eventual free; an interior pointer is a temporary access path, not an independent owner. Do not free an interior pointer or free the same allocation through two copied owners. A borrowed view becomes invalid when the allocation is freed or successfully resized in a way that ends the old allocation’s lifetime. Document whether a function borrows the array, mutates it, or transfers ownership.
A failure-safe reserve function should leave existing elements accessible through the old owner if a nonzero realloc fails. On success, publish the returned base and new capacity together, preserving the logical length. Tests can inject a controlled failure in an allocator abstraction; do not exhaust the real allocator or run use-after-free to claim a fixed outcome. After the owner is released, clear state according to the interface and do not read stale aliases.
Test growth and failure boundaries
Test an empty representation, append within spare capacity, append at full capacity with successful growth, and a forced growth failure. Verify status, length, capacity, base ownership, and existing values as separate observations. Test size-overflow rejection before multiplication and the documented maximum. For a saved position, retain an index, validate it against the logical length after resize, and access through the current base.
The complete programs below use small synthetic allocations and check every allocation result. They were compiled and run locally in C17 mode with warnings enabled in a temporary directory. Successful realloc examples do not assume whether storage moved; failure scenarios that would require allocator injection are described in exercises rather than presented as observed real failures. No stale or freed pointer is dereferenced.
20 worked examples
1. Represent an empty array
Problem: Inspect a synthetic empty array representation before any allocation.
- The base is null and both element counts are zero.
- The printed state is 1 0 0, and free(NULL) is safe.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
int *base = NULL; size_t length = 0, capacity = 0; printf("%d %zu %zu\n", base == NULL, length, capacity); free(base);
return 0;
}
1 0 0
Why this result follows: The empty representation is an explicit interface policy.
2. Append within capacity
Problem: Append one value to an allocated synthetic array with spare capacity.
- The initial length zero is below capacity two.
- The program writes index zero and increments length to one.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t capacity = 2, length = 0; int *base = malloc(capacity * sizeof *base); if (!base) return 1; base[length++] = 7; printf("%zu %zu %d\n", length, capacity, base[0]); free(base);
return 0;
}
1 2 7
Why this result follows: The write occurs within allocated storage and the owner is released once.
3. Fill the last slot
Problem: Append a value when one allocated slot remains.
- Length one identifies the second valid slot in capacity two.
- The append stores seven and makes length equal capacity.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t capacity = 2, length = 1; int *base = malloc(capacity * sizeof *base); if (!base) return 1; base[0] = 4; base[length++] = 7; printf("%zu %d %d\n", length, base[0], base[1]); free(base);
return 0;
}
2 4 7
Why this result follows: The length invariant remains valid at the full boundary.
4. Detect a full array
Problem: Check the growth condition for a fixed full logical array.
- Length and capacity are both two.
- Their equality indicates that no spare slot remains.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t length = 2, capacity = 2; printf("%d\n", length == capacity);
return 0;
}
1
Why this result follows: A full check can trigger reserve before an append writes.
5. Grow a fixed small capacity
Problem: Calculate a doubled capacity for a known safe synthetic value.
- The fixed capacity two is far below the size_t limit.
- Doubling it gives four elements.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t capacity = 2; size_t new_capacity = capacity * 2; printf("%zu\n", new_capacity);
return 0;
}
4
Why this result follows: This fixture proves its small multiplication safe without generalizing to arbitrary capacities.
6. Check a byte-size bound
Problem: Verify a small element count before multiplying by element size.
- sizeof *base measures one int and is nonzero.
- The fixed count four passes the size_t quotient check locally.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t count = 4; int *base = NULL; printf("%d\n", count <= SIZE_MAX / sizeof *base);
return 0;
}
1
Why this result follows: Checking before multiplication prevents allocation-size wrap for accepted counts.
7. Reject a large element count
Problem: Check a maximum synthetic count before forming its byte-size product.
- The local int size exceeds one byte.
- The maximum count exceeds the safe quotient, so the check rejects it.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t count = SIZE_MAX; int *base = NULL; printf("%d\n", count > SIZE_MAX / sizeof *base);
return 0;
}
1
Why this result follows: The program does not evaluate the overflowing size product.
8. Grow with a temporary owner
Problem: Resize a synthetic two-element allocation and initialize the added element.
- The nonzero realloc result is stored in tmp and checked.
- After success the current base is updated and index two is initialized.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t capacity = 2; int *base = malloc(capacity * sizeof *base); if (!base) return 1; base[0] = 4; base[1] = 5; int *tmp = realloc(base, 4 * sizeof *base); if (!tmp) { free(base); return 1; } base = tmp; capacity = 4; base[2] = 6; printf("%zu %d %d %d\n", capacity, base[0], base[1], base[2]); free(base);
return 0;
}
4 4 5 6
Why this result follows: The owner is replaced only on success and all reads use the current allocation.
9. Keep an index across growth
Problem: Save a valid index before resizing and access through the returned base.
- The saved index one identifies an existing element.
- After successful realloc, base[index] reads five from the current allocation.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t capacity = 2; int *base = malloc(capacity * sizeof *base); if (!base) return 1; base[0] = 4; base[1] = 5; size_t index = 1; int *tmp = realloc(base, 4 * sizeof *base); if (!tmp) { free(base); return 1; } base = tmp; printf("%d\n", base[index]); free(base);
return 0;
}
5
Why this result follows: An index can be reapplied to the returned base without using a stale interior pointer.
10. Shrink and retain a prefix
Problem: Shrink a synthetic allocation and read only the two retained elements.
- The first two initialized elements lie within the new nonzero size.
- After success the current base yields one and two.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
int *base = malloc(3 * sizeof *base); if (!base) return 1; base[0] = 1; base[1] = 2; base[2] = 3; int *tmp = realloc(base, 2 * sizeof *base); if (!tmp) { free(base); return 1; } base = tmp; printf("%d %d\n", base[0], base[1]); free(base);
return 0;
}
1 2
Why this result follows: The program uses the realloc result and stays inside the resized allocation.
11. Check the length invariant
Problem: Verify a synthetic array state with spare capacity.
- The logical length two does not exceed capacity four.
- The invariant comparison is true.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t length = 2, capacity = 4; printf("%d\n", length <= capacity);
return 0;
}
1
Why this result follows: Length must remain within capacity before indexed operations.
12. Detect an invalid state
Problem: Check a synthetic state whose logical length exceeds capacity.
- Five is greater than four.
- The invariant comparison fails without accessing any array.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t length = 5, capacity = 4; printf("%d\n", length <= capacity);
return 0;
}
0
Why this result follows: An invalid state can be rejected before it causes an out-of-bounds access.
13. Choose an initial capacity
Problem: Select a fixed nonzero starting capacity for an empty synthetic array.
- The old capacity is zero.
- The conditional policy selects two as its initial capacity.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t capacity = 0; size_t new_capacity = capacity == 0 ? 2 : capacity; printf("%zu\n", new_capacity);
return 0;
}
2
Why this result follows: An explicit empty-to-nonempty rule avoids multiplying zero indefinitely.
14. Borrow a live view
Problem: Read two synthetic elements through an alias before releasing the owner.
- The borrowed pointer designates the live allocation.
- It reads three and four before the owner is freed.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
int *base = malloc(2 * sizeof *base); if (!base) return 1; base[0] = 3; base[1] = 4; int *borrowed = base; printf("%d\n", borrowed[0] + borrowed[1]); free(base);
return 0;
}
7
Why this result follows: A borrowed view can access live storage without becoming a second owner.
15. Clear the released owner
Problem: Release a synthetic base pointer and clear its owner variable.
- The successful allocation is freed once.
- The owner variable is then set to NULL and compares equal to it.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
int *base = malloc(sizeof *base); if (!base) return 1; *base = 9; free(base); base = NULL; printf("%d\n", base == NULL);
return 0;
}
1
Why this result follows: Clearing the owner documents the empty state but does not repair other aliases.
16. Read only initialized logical values
Problem: Sum the first two logical elements of a larger allocation.
- The initialized logical prefix contains two and three.
- Their sum is five; capacity beyond the logical length is not needed.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
int *base = malloc(3 * sizeof *base); if (!base) return 1; base[0] = 2; base[1] = 3; base[2] = 4; size_t length = 2; if (length == 2) printf("%d\n", base[0] + base[1]); free(base);
return 0;
}
5
Why this result follows: Logical length determines which elements belong to the operation.
17. Reject an invalid saved index
Problem: Check a saved index against logical length before access.
- The index two equals the logical length two.
- The strict comparison is false, so no element is accessed.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
int *base = malloc(2 * sizeof *base); if (!base) return 1; base[0] = 1; base[1] = 2; size_t length = 2; size_t index = 2; printf("%d\n", index < length); free(base);
return 0;
}
0
Why this result follows: Valid logical indices are less than length, not equal to it.
18. Check doubling before multiplication
Problem: Check that a small synthetic capacity can be doubled in size_t.
- Three is within half of the local SIZE_MAX.
- The comparison succeeds before any doubling operation.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
size_t capacity = 3; printf("%d\n", capacity <= SIZE_MAX / 2);
return 0;
}
1
Why this result follows: A precondition can reject capacities that would overflow when doubled.
19. Transfer the current owner
Problem: Move a synthetic allocation to a new owner variable.
- The new variable receives the live base and the former owner is cleared.
- The new owner reads five and releases the allocation once.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
int *base = malloc(sizeof *base); if (!base) return 1; *base = 5; int *new_owner = base; base = NULL; printf("%d\n", *new_owner); free(new_owner);
return 0;
}
5
Why this result follows: An explicit transfer identifies which variable is responsible for free.
20. Keep a value after release
Problem: Copy a small aggregate before releasing its synthetic array.
- The initialized elements add to five while storage is live.
- The array is freed, then the independent sum is printed.
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
int main(void) {
int *base = malloc(2 * sizeof *base); if (!base) return 1; base[0] = 2; base[1] = 3; int sum = base[0] + base[1]; free(base); printf("%d\n", sum);
return 0;
}
5
Why this result follows: A copied scalar result does not require accessing the freed allocation.
Common errors and fixes
The allocation is leaked on failure
- Why it happens
- The only pointer was overwritten by a null result.
- How to fix it
- Store the result in a temporary pointer.
- Prevent it next time
- Review both ownership paths.
An old element pointer is used after growth
- Why it happens
- Successful realloc ended the old allocation.
- How to fix it
- Recompute the pointer from the new base.
- Prevent it next time
- Keep indices across resizing.
The requested byte count wraps
- Why it happens
- The product exceeds SIZE_MAX.
- How to fix it
- Reject the count before multiplication.
- Prevent it next time
- Test arithmetic boundaries.
20 practice exercises
1. What happens to the original allocation when a nonzero realloc request fails?
Show hint
Keep the original owner until a nonzero realloc request succeeds.
Show worked solution
It remains allocated and the original pointer is still needed to release it. The temporary result is null.
If a nonzero realloc request returns NULL, the original allocation remains allocated and its original pointer is still needed for access or release. Store the result in a temporary pointer and update the owner only after success.
Check your solution
- The temporary result is NULL on failure
- The original allocation remains owned
2. Why store an element index across a resize?
Show hint
Recompute an element address from the current base after successful growth.
Show worked solution
A successful resize can move storage. A validated index can be applied to the returned base, whereas an old interior pointer must not be used.
A successful resize may move storage and invalidate old interior access paths. Preserve a validated element index, then apply it to the returned base after checking it remains within the logical length.
Check your solution
- The index is validated against current length
- No old interior pointer is used after successful resize
3. An empty array uses base NULL, length 0, and capacity 0. State its ownership policy.
Show hint
Describe the null state and later release responsibility.
Show worked solution
This policy represents no allocation with a null base and zero counts. A successful later allocation becomes the owner’s responsibility, and free(NULL) is safe during cleanup of the empty state.
Check your solution
- The empty state has zero length and capacity
- The current owner is released once
4. A resizable array has length 1 and capacity 3. Where can the next append write?
Show hint
The next element index is the current length.
Show worked solution
The next append can write index 1 because 1 is less than capacity 3. After the write, length becomes 2. The existing element at index 0 remains within the logical sequence.
Check your solution
- The write index is 1
- The new length is 2
5. An array has length 2 and capacity 2. What must happen before writing a third element?
Show hint
No spare slot remains.
Show worked solution
Reserve a capacity of at least 3 successfully before writing index 2. If growth fails and the contract promises unchanged state, preserve the original owner, length, capacity, and existing values.
Check your solution
- Growth succeeds before the write
- Failure preserves the documented state
6. State the check needed before calculating count * sizeof *base for an allocation.
Show hint
Compare count against the maximum quotient first.
Show worked solution
Reject count > SIZE_MAX / sizeof *base before multiplying. If the check passes, the byte-size product is representable in size_t. Handle a zero-count policy separately from this arithmetic check.
Check your solution
- The bound is checked before multiplication
- The element size is sizeof *base
7. A growth policy doubles capacity. Why must it check the capacity before multiplying by two?
Show hint
Unsigned size_t arithmetic can wrap.
Show worked solution
A capacity greater than SIZE_MAX / 2 would overflow the intended doubled size, even though unsigned arithmetic wraps. Reject or choose another documented bound before multiplication so a smaller accidental allocation is not published.
Check your solution
- The doubling bound is checked first
- A wrapped capacity is not accepted
8. For a nonzero realloc request, explain what happens to the original allocation if realloc returns NULL.
Show hint
Keep the original owner in a separate variable.
Show worked solution
The original allocation remains allocated and its original pointer is still needed for access or free. A temporary result is NULL. Do not overwrite the sole owner until a new pointer is returned successfully.
Check your solution
- The original allocation remains owned
- The temporary result is NULL
9. Explain why assigning realloc directly to the sole base pointer can lose an allocation on failure.
Show hint
A null result can overwrite the only stored owner.
Show worked solution
If the nonzero request fails, realloc returns NULL while the original allocation remains live. Direct assignment loses the only pointer to it. Store the result in tmp and update base only on success.
Check your solution
- The original base is retained on failure
- The temporary result is checked
10. A successful resize may move storage. Why retain index 1 instead of an interior pointer?
Show hint
Recompute access from the returned base after validation.
Show worked solution
A successful realloc can end the old allocation’s lifetime, making an old interior pointer unsuitable. Save index 1, verify it is below the current logical length, and access base[1] using the returned base.
Check your solution
- The saved index is validated
- Access uses the current returned base
11. An array grows from capacity 2 to 4. What must happen before reading newly added element 2?
Show hint
New realloc storage is not automatically initialized.
Show worked solution
After successful growth, initialize base[2] before reading it. Use the returned pointer as the current owner and preserve the existing logical length until the new element is deliberately added.
Check your solution
- The current returned base is used
- Element 2 is initialized before reading
12. A resize request fails under an unchanged-on-failure contract. Which state must a test inspect?
Show hint
Status alone does not establish preservation.
Show worked solution
Assert failure and verify that the original base remains owned, length and capacity retain their prior values, and existing elements are unchanged. Inject the failure in a controlled fixture rather than exhausting real memory.
Check your solution
- The original owner remains available
- Length, capacity, and existing values are unchanged
13. A zero-capacity API chooses NULL with length zero. Why avoid relying on realloc(base, 0)?
Show hint
Zero-size behavior needs an explicit policy.
Show worked solution
Define the empty representation and release behavior directly rather than using a zero-size realloc as the interface policy. This makes the result and ownership clear across implementations and failure paths.
Check your solution
- The empty representation is documented
- No zero-size realloc behavior is assumed
14. A borrowed pointer points inside an allocated array. May it be freed as an independent owner?
Show hint
Only the appropriate allocation owner is released.
Show worked solution
No. An interior pointer is an access path, not the allocation pointer returned as owner. Release the current base once according to the contract and stop using borrowed aliases afterward.
Check your solution
- The interior pointer is not freed
- The current base is released once
15. An array has length 2 and capacity 4. Is index 2 a valid logical element?
Show hint
Logical length and storage capacity are different.
Show worked solution
Index 2 is within allocated capacity but is not below logical length 2, so it is not a current logical element. Initialize and append it under the contract before treating it as one.
Check your solution
- Index 2 is outside the logical sequence
- Any new element is initialized before reading
16. A saved index is 1 and the resized array’s logical length remains 2. How should it be accessed?
Show hint
Validate against the current length and use the current base.
Show worked solution
Index 1 is below length 2, so it identifies a logical element. After successful resize, read base[1] through the returned current base, not through an old interior pointer.
Check your solution
- The index is less than length
- The current base is used
17. Describe a controlled test that forces a reserve failure without exhausting system memory.
Show hint
Inject failure through a test allocator abstraction.
Show worked solution
Configure a synthetic allocator to return failure for the nonzero growth request. Assert failure and compare the old owner, length, capacity, and elements with their pre-call state; then release the owner once.
Check your solution
- The growth failure is injected
- All promised state remains unchanged
18. Why should a capacity field be updated only after successful allocation growth?
Show hint
Published metadata must describe the storage actually owned.
Show worked solution
If capacity is increased before realloc succeeds, a failure can leave metadata claiming space the old allocation does not have. Update the base and capacity only after success, while preserving length.
Check your solution
- Failed growth does not publish larger capacity
- The metadata matches the current allocation
19. An owner is freed and then set to NULL. Does that make a copied alias safe to read?
Show hint
Clearing one variable does not change other copies.
Show worked solution
No. The copied alias still refers to the ended allocation and must not be dereferenced. Setting the owner variable to NULL can clarify its state but does not repair aliases.
Check your solution
- The owner variable is NULL
- The stale alias is not read
20. Choose success and boundary tests for appending to an array of capacity 2.
Show hint
Cover spare space, full state, and growth failure.
Show worked solution
Test append from length 0, append from length 1 into the last slot, and append from length 2 requiring growth. Inject a growth failure and check status plus preserved state as well as the successful paths.
Check your solution
- Both spare-capacity cases succeed
- Growth failure preserves the documented state
Key takeaways
- Use a temporary pointer for nonzero realloc requests.
- Check size arithmetic and refresh pointers after success.
- A failed nonzero realloc leaves the original allocation owned and available.
- A successful resize requires using the returned base rather than stale interior pointers.
- Check capacity arithmetic before multiplication and publish metadata only on success.