Dynamic Memory Allocation in C Using Malloc
Dynamic Memory Allocation in C Using Malloc
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,
If there is a situation where only 5 elements are needed to be entered in
this array. In this case, the remaining 4 indices are just wasting memory in
this array.
So, there is a requirement to lessen the length (size) of the array from 9 to
5.
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.
Syntax:
ptr = (cast-type*) malloc(byte-size)
For Example:
ptr = (int*) malloc(100 * sizeof(int));
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.
1. Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
return 0;
}
Output:
2. C calloc() method
Syntax:
ptr = (cast-type*)calloc(n, element-size);
For Example:
1. Example:
#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,
3. C free() method
Syntax:
free(ptr);
4. C realloc() method
Syntax:
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
free(ptr);
}
return 0;
}
Output:
Drawbacks:
1) Random access is not allowed. We have to access elements sequentially starting from the
first node. So we cannot do binary search with linked lists efficiently with its default
implementation. Read about it here.
2) Extra memory space for a pointer is required with each element of the list.
3) Not cache friendly. Since array elements are contiguous locations, there is locality of
reference which is not there in case of linked lists.
Single link list :
A linked list is a sequence of data structures, which are connected together via
links.
Linked List is a sequence of links which contains items. Each link contains a
connection to another link.
Linked list is the second most-used data structure after array. Following are the
important terms to understand the concept of Linked List.
Link − Each link of a linked list can store a data called an element.
Next − Each link of a linked list contains a link to the next link called Next.
LinkedList − A Linked List contains the connection link to the first link called First.
As per the above illustration, following are the important points to be considered.
Linked List contains a link element called first.
Each link carries a data field(s) and a link field called next.
Each link is linked with its next link using its next link.
Last link carries a link as null to mark the end of the list.
As per the above illustration, following are the important points to be considered.
Doubly Linked List contains a link element called first and last.
Each link carries a data field(s) and two link fields called next and prev.
Each link is linked with its next link using its next link.
Each link is linked with its previous link using its previous link.
The last link carries a link as null to mark the end of the list.
Circular Linked List is a variation of Linked list in which the first element points to
the last element and the last element points to the first element. Both Singly Linked
List and Doubly Linked List can be made into a circular linked list.
As per the above illustration, following are the important points to be considered.
The last link's next points to the first link of the list in both cases of singly as well as
doubly linked list.
The first link's previous points to the last of the list in case of doubly linked list.
Circular Linked List − Last item contains link of the first element as next and the first
element has a link to the last element as previous