In case of static memory allocation there are some drawbacks like: wastage of memory, less flexibility, permanent allocation of variables etc. To overcome these situations the concept of Dynamic Memory Allocation has been introduced. In dynamic memory allocation the memories can be allocated dynamically at run time.
There 4 library functions that are described under <stdlib.h> (Standard Library Functions)
1. malloc()
2. calloc()
3. realloc()
4. free()
In this article we’ll elaborately learn about free().
What is free()?
In dynamic memory allocation the memory have to be deallocated explicitly. It releases all the used memory spaces previously allocated by calloc(), malloc() or realloc() functions. If we no longer need the data stored in a particular block of memory, then we should have a practice to release that memory for future use. After freeing the memory blocks the memories are returned to heap.
free() is used to save unused memory by freeing it. Dynamic allocation have this advantage which static allocation lacks.
Syntax
free(ptr);
Lets take an example to understand.
/* C code to execute free() in DMA */
/* www.codewindow.in */
#include <stdio.h>
#include <stdlib.h>
// Driver code
int main() {
int *p, n; // Pointer and the variables are initialized
printf("Enter the size of array: ");
scanf("%d", &n);
p = (int*) calloc(n, sizeof(int)); // An int array of n elements has been assigned to the pointer
if(p==NULL) {
printf("\nMemory allocation unsuccessful using calloc");
exit(0);
}
else {
printf("\nMemory allocation successful using calloc");
printf("\nEnter the elements of the array: ");
for(int i=0; i<n; ++i)
scanf("%d", p+i); // Array elements are taken as input
printf("\nThe elements of the array: ");
for(int i=0; i<n; ++i)
printf("%d ", *(p + i)); // Print element of the array
free(p); //frees the space allocated in the memory
printf("\nMemory has been freed successfully using free");
}
return 0;
}
Input
Enter the size of array: 5
Enter the elements of the array: 1 2 3 4 5
Output
Memory allocation successful using calloc
The elements of the array: 1 2 3 4 5
Memory has been freed successfully using free
Explanation
Previously allocated memory was 5 * 4[no. of elements * sizeof(int)] = 20 bytes using calloc. By using the function “free()”, the space allocated to the memory has been deleted thereafter.
Follow Us
You Missed
- Dunzo Off-Campusing Drive | Data Engineer | codewindow.in
- Verbal Ability Questions Solved | Synonyms Antonyms – codewindow.in
- Verbal Ability Questions Solved | Statement Correction – codewindow.in
- Verbal Ability Questions Solved | Spotting Error – codewindow.in
- Verbal Ability Questions Solved | Speech and Tenses – codewindow.in
- Try
- Verbal Ability Questions Solved | Sentence Arrangement – codewindow.in
- Verbal Ability Questions Solved | Prepositions – codewindow.in
- Verbal Ability Questions Solved | Grammar – codewindow.in
- Verbal Ability Questions Solved | Articles – codewindow.in
- Zoho Off Campus Drive | Product Marketing Associate / Specialist | codewindow.in
- BerryWorks Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- Microsoft Off Campus Hiring Drive – Technical Support – Codewindow.in
- Infosys Pseudocede & Puzzle Solved – codewindow.in
- Infosys Verbal Questions Solved – codewindow.in
- Amazon Off Campus Drive | Trainee | codewindow.in
- Hawkins Off Campus Drive | Management trainees | codewindow.in
- Infosys Mathematical Questions Solved – codewindow.in
- Paypal Off Campus Recruitment Drive – Software Engineer – Codewindow.in
- IBM Off Campus Hiring Drive – Associate Systems Engineer – Codewindow.in
- Infosys Off Campus Hiring Drive – SP and DSE – Codewindow.in
- VIRTUSA Recruitment 2022 | Associate Engineer | codewindow.in
- Infosys Logical Questions solved – codewindow.in
- Google Off Campus Hiring Drive – IT Support Engineer – Codewindow.in
- Capgemini Pseudocode Solved | Set 4 – codewindow.in
- Capgemini Pseudocode Solved | Set 3 – codewindow.in
- Capgemini Pseudocode Solved | Set 2 – codewindow.in
- JECA 2022 Question Paper – 90+ Sample Questions Exposed – codewindow.in
- Capgemini Pseudocode Solved | Set 1 – codewindow.in
- Verizon Off Campusing | Software Developer | codewindow.in
- TCS Recruitment Off-Campusing | Service Desk Analyst | codwindow.in
- Amdocs Off-Campusing | Devops Engineer | codewindow.in
Also Checkout
- Algorithm (6)
- Aptitude (7)
- Capgemini Coding Questions (2)
- Capgemini Pseudocode (4)
- CodeVita (3)
- Coding Questions (246)
- Cognizant Placement (11)
- Data Structure and Algorithm (69)
- Epam Full Question Paper (6)
- Guidance for Accenture (2)
- HR Questions (11)
- IBM Questions (5)
- Infosys (10)
- Internship (1)
- Interview Experience (27)
- JECA (1)
- Job Info (398)
- Machine Learning (1)
- Miscellaneous (65)
- NPCI (2)
- Programming in C (58)
- Programming in C++ (6)
- Programming in JAVA (17)
- Programming in Python (132)
- Quiz (9)
- Recruiting Companies (36)
- Revature (3)
- Study Material (4)
- TCS NQT (64)
- Tech Mahindra Coding Questions (4)
- Tech Mahindra Questions (8)
- Uncategorized (62)
- Verbal Ability (8)
- Web Development (18)
- Wipro Coding Questions (1)
- Wipro NLTH (46)
- WIpro NLTH Coding Solve (31)