Overview
- Topic: Dynamic memory reallocation using realloc in C.
- Purpose: Explain realloc usage, behavior, and a simple example demonstrating increasing allocated memory without losing existing data.
realloc: Definition and Purpose
- realloc is a standard library function declared in stdlib.h.
- Purpose: Change size of an existing memory block without losing old data (when possible).
- Return type: void* (pointer to the resized memory block).
- Arguments: (1) pointer to previously allocated memory, (2) new size in bytes.
- On failure, realloc returns NULL.*
Key Behaviors and Rules
- realloc may allocate a new block and move existing contents to it.
- If new size is greater than old size, existing data is preserved.
- If new size is smaller than old size, data beyond the new size may be lost.
- Newly allocated bytes (the additional space) are uninitialized and must be initialized by the programmer.
- Always check returned pointer for NULL to detect allocation failure.
Syntax and Simple Example Explanation
- Typical syntax: ptr = realloc(ptr, new_size);
- Example scenario:
- Initially allocate memory for 2 integers: ptr = malloc(2 * sizeof(int)); (assume sizeof(int) = 4 bytes → 8 bytes)
- Later, reallocate to hold 4 integers: ptr = realloc(ptr, 4 * sizeof(int)); (now 16 bytes)
- Pass the original ptr as first argument so realloc can move old contents if needed.
Example Program Flow (step-by-step)
- Allocate memory for two integers and store returned pointer in ptr.
- Check if ptr == NULL; if so, exit with failure.
- Read two integers from user and store in the first two slots.
- Call realloc(ptr, 4 * sizeof(int)) to expand to space for two more integers.
- Check realloc return for NULL; if NULL, handle memory allocation failure.
- If successful, read two additional integers into indices 2 and 3 (loop starts at i = 2).
- Print all four integers to verify data preservation and new inputs.*
Important Notes For Students
- Always include stdlib.h for malloc, realloc, free.
- Always check returned pointer from malloc/realloc for NULL before using memory.
- After successful realloc, previous pointer value may be invalid; use the returned pointer.
- Initialize newly allocated memory if values are required to be defined.
- When shrinking memory, be cautious: data can be truncated.
Key Terms and Definitions
| Term | Definition |
| realloc | Function to resize an allocated memory block, preserving data when possible. |
| malloc | Function to allocate a block of memory of specified size on the heap. |
| NULL return | Indicator that memory allocation (malloc/realloc) failed. |
| Uninitialized bytes | Newly allocated memory contents that contain indeterminate values and must be set. |
Action Items / Next Steps
- Practice: Write programs that expand and shrink arrays using realloc and observe behavior.
- Safety checks: Always verify returned pointers from malloc/realloc before use.
- Initialization: Initialize newly added elements after realloc before using them.
- Free memory: Remember to free allocated memory when no longer needed to avoid leaks.