The free function is used to give back memory that was taken before. This helps your program not waste memory and run better.
free function
Introduction
Syntax
C
void free(void *ptr);
The ptr must be a pointer you got from malloc, calloc, or realloc.
After calling free, the pointer ptr becomes invalid and should not be used again unless assigned a new value.
Examples
C
int *p = malloc(sizeof(int)); free(p);
C
char *name = calloc(10, sizeof(char)); free(name);
Sample Program
This program allocates memory for 5 integers, fills them with values, prints them, and then frees the memory.
C
#include <stdio.h> #include <stdlib.h> int main() { int *numbers = malloc(5 * sizeof(int)); if (numbers == NULL) { printf("Memory allocation failed\n"); return 1; } for (int i = 0; i < 5; i++) { numbers[i] = i * 10; } for (int i = 0; i < 5; i++) { printf("%d ", numbers[i]); } printf("\n"); free(numbers); // Give back the memory return 0; }
Important Notes
Always check if malloc or calloc returned NULL before using the memory.
Do not call free on memory that was not allocated dynamically or already freed.
Summary
free returns memory you no longer need back to the system.
Use it after malloc, calloc, or realloc to avoid memory waste.
Never use the pointer after freeing it without assigning new memory.
Practice
1. What is the main purpose of the
free function in C?easy
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]
Hint: Remember: free returns memory, malloc gets memory [OK]
Common Mistakes:
- Confusing free with malloc or calloc
- Thinking free allocates memory
- Using free to copy data
2. Which of the following is the correct way to free memory allocated by
malloc?easy
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]
Hint: Always set pointer to NULL after free to avoid bugs [OK]
Common Mistakes:
- Calling free twice on the same pointer
- Incrementing pointer after free
- Reassigning pointer without freeing first
3. What will be the output of this C code?
char *ptr = malloc(5);
strcpy(ptr, "test");
free(ptr);
printf("%s", ptr);medium
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]
Hint: Never use pointer after free without reassigning [OK]
Common Mistakes:
- Assuming freed pointer still holds valid data
- Expecting compilation error
- Thinking it prints empty string
4. Identify the error in this code snippet:
int *arr = malloc(3 * sizeof(int)); free(arr + 1);
medium
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]
Hint: Free only original malloc pointer, not offset pointers [OK]
Common Mistakes:
- Freeing pointer offset from malloc result
- Not freeing allocated memory
- Assuming free can take any pointer inside block
5. You have a function that reallocates memory for an integer array:
What is the best practice to avoid memory leaks when using this function?
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?
hard
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]
Hint: Use temp pointer for realloc result before replacing original [OK]
Common Mistakes:
- Overwriting original pointer without checking realloc result
- Freeing original pointer before realloc
- Ignoring NULL return from realloc
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
