Implement append_int so a heap-backed buffer grows geometrically, checks multiplication overflow, and leaves the original allocation untouched if realloc fails.
Requirements
Use realloc only through a temporary
Maintain size <= capacity
Free the buffer at the caller's end of life
Example
Input
append 4 values
Expected output
size 4
Show a hint
Write down the input contract and the failure cases before coding.
local runtime
RUNTIME OUTPUT
Use your C runtime locally, then compare with solution.c.
Use your local runtime, then review the reference solution
LEARN FROM THE SOLUTION
Why the solution works.
A temporary pointer is essential: assigning realloc directly would lose the original allocation on failure. Geometric growth keeps append amortized O(1), while overflow checks ensure the byte count passed to realloc is representable. The caller remains responsible for free(buffer.data).
What this exercise teaches
realloc ownership
Capacity invariants
Overflow-aware allocation
A PRACTICAL PLAN
Work through Grow a dynamic integer buffer with intent.
Translate the contract.Turn the requirements into a short checklist before editing your-solution.c.
Use the example as evidence.Predict the result for the supplied input, then add one boundary case such as an empty value, a limit, or unexpected input.
Review the implementation.Compare your choices against solution.c only after a real attempt.
EXERCISE FAQ
Before you move on.
What does “Grow a dynamic integer buffer” teach?
This intermediate C exercise focuses on realloc ownership, Capacity invariants, Overflow-aware allocation. Its requirements define the exact behavior to implement before you write code.
How should I validate this C solution?
Start with the displayed example input and expected output, then test a boundary case suggested by the requirements. Write and compile the starter in your local C environment, exercise the example and edge cases, then compare your approach with solution.c.
When should I open the reference solution?
Attempt Grow a dynamic integer buffer first. Then open the read-only solution file to compare the contract, edge-case handling, and implementation choices—not simply to copy the final code.