Beruflich Dokumente
Kultur Dokumente
Custom Search
perm_identity
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
Since C is a structured language, it has some fixed rules for programming. One of
it includes changing the size of an array. An array is collection of items stored
at continuous memory locations.
arrays
As it can be seen that the length (size) of the array above made is 9. But what if
there is a requirement to change this length (size). For Example,
Take another situation. In this, there is an array of 9 elements with all 9 indices
filled. But there is a need to enter 3 more elements in this array. In this case 3
indices more are required. So the length (size) of the array needs to be changed
from 9 to 12.
This procedure is referred to as Dynamic Memory Allocation in C.
malloc()
calloc()
free()
realloc()
Let�s look at each of them in greater detail.
C malloc() method
�malloc� or �memory allocation� method in C is used to dynamically allocate a
single large block of memory with the specified size. It returns a pointer of type
void which can be cast into a pointer of any form.
Syntax:
Since the size of int is 4 bytes, this statement will allocate 400 bytes of memory.
And, the pointer ptr holds the address of the first byte in the allocated memory.
Example:
filter_none
edit
play_arrow
brightness_5
#include <stdio.h>
#include <stdlib.h>
int main()
{
return 0;
}
Output:
Enter number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,
C calloc() method
�calloc� or �contiguous allocation� method in C is used to dynamically allocate the
specified number of blocks of memory of the specified type. It initializes each
block with a default value �0�.
Syntax:
This statement allocates contiguous space in memory for 25 elements each with the
size of the float.
Example:
filter_none
edit
play_arrow
brightness_5
#include <stdio.h>
#include <stdlib.h>
int main()
{
return 0;
}
Output:
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
C free() method
�free� method in C is used to dynamically de-allocate the memory. The memory
allocated using functions malloc() and calloc() is not de-allocated on their own.
Hence the free() method is used, whenever the dynamic memory allocation takes
place. It helps to reduce wastage of memory by freeing it.
Syntax:
free(ptr);
Example:
filter_none
edit
play_arrow
brightness_5
#include <stdio.h>
#include <stdlib.h>
int main()
{
return 0;
}
Output:
Enter number of elements: 5
Memory successfully allocated using malloc.
Malloc Memory successfully freed.
Syntax:
ptr = realloc(ptr, newSize);
Example:
filter_none
edit
play_arrow
brightness_5
#include <stdio.h>
#include <stdlib.h>
int main()
{
free(ptr);
}
return 0;
}
Output:
Enter number of elements: 5
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3, 4, 5,
Recommended Posts:
C | Dynamic Memory Allocation | Question 6
C | Dynamic Memory Allocation | Question 7
C | Dynamic Memory Allocation | Question 8
C | Dynamic Memory Allocation | Question 5
C | Dynamic Memory Allocation | Question 2
C | Dynamic Memory Allocation | Question 3
C | Dynamic Memory Allocation | Question 1
C | Dynamic Memory Allocation | Question 8
calloc() versus malloc()
How to restrict dynamic allocation of objects in C++?
MCQ on Memory allocation and compilation process
How to deallocate memory without using free() in C?
How does free() know the size of memory to be deallocated?
Use of realloc()
malloc() vs new
RishabhPrabhu
Technical Content Engineer at GeeksForGeeks
If you like GeeksforGeeks and would like to contribute, you can also write an
article using contribute.geeksforgeeks.org or mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main
page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the
"Improve Article" button below.
Article Tags : CC BasicsDynamic Memory Allocation
Practice Tags : C
thumb_up
48
2.2
Based on 15 vote(s)
Feedback/ Suggest ImprovementAdd NotesImprove Article
Please write to us at contribute@geeksforgeeks.org to report any issue with the
above content.
Post navigation
Previous
first_page Assignment Operators in C/C++
Next
last_pageBasic Input and Output in C
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share
the link here.
Load Comments
auto
Most popular articles
Must Do Coding Questions for Companies like Amazon, Microsoft, Adobe, ...
D3.js | d3.entries() Function
How to pass integer by reference in Java
Find the winner of the Game to Win by erasing any two consecutive similar alphabets
Find the integers that doesnot ends with T1 or T2 when squared and added X
?
Write a Testimonial
?
GeeksforGeeks
5th Floor, A-118,
Sector-136, Noida, Uttar Pradesh - 201305
feedback@geeksforgeeks.org
COMPANY
About Us
Careers
Privacy Policy
Contact Us
LEARN
Algorithms
Data Structures
Languages
CS Subjects
Video Tutorials
PRACTICE
Courses
Company-wise
Topic-wise
How to begin?
CONTRIBUTE
Write an Article
Write Interview Experience
Internships
Videos