What happens if your program never puts back the memory it borrows?
Why free function? - Purpose & Use Cases
Imagine you are managing a big box of toys. Every time you take a toy out, you need to remember to put it back in the box when done. If you forget, the toys pile up and clutter your room.
Without a clear way to put toys back, your room becomes messy. Similarly, in C programming, if you don't release memory you no longer need, your program uses more and more memory, slowing down or crashing.
The free function helps you put back the memory you took. It cleans up the space so your program stays fast and tidy, just like putting toys back keeps your room neat.
char *name = malloc(100);
// use name
// forget to free memorychar *name = malloc(100);
// use name
free(name);Using free lets your program run smoothly without running out of memory, even when handling many tasks.
Think of a video game that loads many levels and characters. Without freeing memory, the game would slow down or crash after a while. Using free keeps the game running fun and smooth.
Memory must be given back when no longer needed.
free helps prevent memory waste and crashes.
It keeps programs efficient and reliable.
Practice
free function in C?Solution
Step 1: Understand what
Thefreedoesfreefunction releases memory that was allocated dynamically, returning it to the system.Step 2: Differentiate from other memory functions
mallocallocates memory,freereleases it. Copying or initializing pointers is unrelated.Final Answer:
To release memory previously allocated bymallocor similar functions -> Option DQuick Check:
free releases memory = A [OK]
- Confusing free with malloc or calloc
- Thinking free allocates memory
- Using free to copy data
malloc?Solution
Step 1: Free the allocated memory
Callingfree(pointer);releases the memory correctly.Step 2: Avoid dangling pointer
Settingpointer = NULL;after freeing prevents accidental use of freed memory.Final Answer:
free(pointer); pointer = NULL; -> Option AQuick Check:
Free then NULL pointer = D [OK]
- Calling free twice on the same pointer
- Incrementing pointer after free
- Reassigning pointer without freeing first
char *ptr = malloc(5);
strcpy(ptr, "test");
free(ptr);
printf("%s", ptr);Solution
Step 1: Analyze memory allocation and free
Memory is allocated and string "test" copied. Then memory is freed.Step 2: Using pointer after free
Printingptrafter free causes undefined behavior; it may print garbage or crash.Final Answer:
Undefined behavior (may print garbage or crash) -> Option BQuick Check:
Using freed pointer = undefined behavior [OK]
- Assuming freed pointer still holds valid data
- Expecting compilation error
- Thinking it prints empty string
int *arr = malloc(3 * sizeof(int)); free(arr + 1);
Solution
Step 1: Understand pointer passed to free
Memory was allocated atarr, but free is called onarr + 1, which is not the original pointer.Step 2: Free must be called on original pointer
Only the pointer returned by malloc (or NULL) can be passed to free safely.Final Answer:
free called on pointer not returned by malloc -> Option AQuick Check:
Free must use original pointer = A [OK]
- Freeing pointer offset from malloc result
- Not freeing allocated memory
- Assuming free can take any pointer inside block
int *resize(int *arr, int new_size) {
int *temp = realloc(arr, new_size * sizeof(int));
if (temp == NULL) {
free(arr);
return NULL;
}
return temp;
}What is the best practice to avoid memory leaks when using this function?
Solution
Step 1: Understand realloc behavior
If realloc fails, it returns NULL and original memory remains allocated.Step 2: Use temporary pointer to avoid losing original memory
Assign realloc result to a temp pointer first; if NULL, free original to avoid leaks.Final Answer:
Assign the return value to a temporary pointer before updating original pointer -> Option CQuick Check:
Use temp pointer for realloc to prevent leaks = B [OK]
- Overwriting original pointer without checking realloc result
- Freeing original pointer before realloc
- Ignoring NULL return from realloc
Ready to prove it?
Pass the test and this pattern moves unknown
