0
0
Cprogramming~10 mins

realloc function - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - realloc function
Start with pointer p
Call realloc(p, new_size)
Check if realloc returned NULL?
YesHandle error, keep old p
No
p now points to resized memory
Use resized memory
Free memory when done
The realloc function tries to resize a memory block pointed by p. If successful, it returns a new pointer; if it fails, it returns NULL and the old pointer remains valid.
Execution Sample
C
int *p = malloc(2 * sizeof(int));
p[0] = 10; p[1] = 20;
int *temp = realloc(p, 4 * sizeof(int));
if (temp != NULL) {
    p = temp;
    p[2] = 30; p[3] = 40;
} else {
    // Handle realloc failure
}
This code allocates memory for 2 integers, stores values, then resizes to hold 4 integers and adds more values safely by checking realloc's return value.
Execution Table
StepActionPointer pMemory SizeResult/Notes
1malloc(2 ints)p -> block12 intsMemory allocated for 2 ints
2Assign p[0]=10, p[1]=20p -> block12 intsValues stored in memory
3realloc(p, 4 ints)p -> block2 or block14 intsMemory resized, pointer updated if successful
4Assign p[2]=30, p[3]=40p -> block2 or block14 intsNew values stored in resized memory
💡 Execution ends after storing new values in resized memory block
Variable Tracker
VariableStartAfter mallocAfter reallocFinal
pNULLAddress block1Address block2 or block1Address block2 or block1
Memory Size02 ints4 ints4 ints
Key Moments - 3 Insights
What happens if realloc returns NULL?
If realloc returns NULL (see step 3 in execution_table), the original pointer p is still valid and points to the old memory. You must not overwrite p without checking, or you lose the reference to allocated memory causing a memory leak.
Does realloc always move memory to a new location?
No, realloc may resize the memory in place (p points to same block) or move it to a new block (p points to new block). This is why p must be assigned the return value of realloc.
Why assign p = realloc(p, new_size) instead of just calling realloc(p, new_size)?
Because realloc returns the new pointer to the resized memory. If you don't assign it, you won't know the new location and may access invalid memory.
Visual Quiz - 3 Questions
Test your understanding
Look at the variable_tracker table, what is the value of p after realloc?
ASame as after malloc or new address
BNULL
CUnchanged NULL
DUndefined
💡 Hint
Check 'After realloc' column for variable p in variable_tracker
At which step in execution_table is the memory resized?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for realloc call in execution_table
If realloc fails and returns NULL, what should you do to avoid losing the original memory?
AAssign p = realloc(p, new_size) without check
BFree p immediately
CUse a temporary pointer to store realloc result first
DIgnore the return value
💡 Hint
Refer to key_moments about handling realloc failure
Concept Snapshot
realloc(pointer, new_size) resizes memory block.
Returns new pointer or NULL if fails.
Always assign result to a temp pointer first.
If NULL, old pointer is still valid.
Use realloc to grow or shrink allocated memory.
Full Transcript
The realloc function in C changes the size of a memory block pointed to by a pointer. First, you allocate memory with malloc. Then you can call realloc to resize that memory. realloc returns a new pointer to the resized memory or NULL if it fails. If it returns NULL, the original pointer remains valid and must not be overwritten to avoid memory leaks. Always check realloc's return value before assigning it to your pointer. This example shows allocating memory for 2 integers, storing values, then resizing to hold 4 integers and adding more values. The execution table traces each step, showing how the pointer and memory size change. Key moments clarify common confusions about realloc's behavior and safe usage.