Dynamic Memory Allocation in C

The notion of dynamic memory allocation in c enables a programmer to allocate memory at runtime. There are 4 library functions provided by C defined under <stdlib.h> header file to facilitate dynamic memory allocation in C programming.
Function Purpose
malloc Allocates the memory of the requested size and returns the pointer to the first byte of allocated space.
calloc Allocates the space for elements of an array. Initializes the elements to zero and returns a pointer to the memory.
realloc It is used to modify the size of a previously allocated memory space.
Free Frees or empties the previously allocated memory space.

Before exploring the above functions, let’s understand the difference between static memory allocation and dynamic memory allocation.

Static vs Dynamic Memory Allocation

Static memory allocation is a method of allocating memory, and once the memory is allocated, it is fixed. Dynamic memory allocation is a method of allocating memory, and once the memory is allocated, it can be changed.
 Modification
In static memory allocation, it is not possible to resize after initial allocation. In dynamic memory allocation, the memory can be minimized or maximize accordingly.
Implementation
Static memory allocation is easy to implement. Dynamic memory allocation is complex to implement.
 Speed
In static memory, allocation execution is faster than dynamic memory allocation. In dynamic memory, allocation execution is slower than static memory allocation.
Memory Utilization
In static memory allocation, cannot reuse the unused memory. Dynamic memory allocation allows reusing the memory. The programmer can allocate more memory when required . He can release the memory when necessary.

Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()

malloc ()

The malloc method is used to dynamically allocate one large block of memory with a given size. It returns a pointer of type void, which can be transmitted by any pointer of any form.

Syntax of malloc:

ptr=(cast-type*)malloc(byte-size)
Example of malloc memory allocation:
#include <stdio.h> 
#include <stdlib.h>

int main() 
{
int* ptr; 
int n, i, sum = 0;
n = 10; 
printf("Please enter number of elements: %d\n", n);
ptr = (int*)malloc(n * sizeof(int));

if (ptr == NULL) { 
printf("Memory not allocated.\n"); 
exit(0); 
} 

else {
printf("Memory successfully allocated using malloc.\n");
for (i = 0; i < n; ++i) { 
ptr[i] = i + 1; 
}
printf("The elements of the array are: "); 
for (i = 0; i < n; ++i) { 
printf("%d, ", ptr[i]); 
} 
}

return 0; 
}

Output:

Please enter number of elements: 3
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3

calloc ()

The “calloc is used to dynamically allocate a specified number of memory blocks of the specified type. It initializes each block with a default value of “0”.

Syntax:

ptr = (type type *) calloc (n, element size);

Example of calloc memory allocation:

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 

int* ptr; 
int n, i, sum = 0; 
n = 5; 
printf("Please enter number of elements: %d\n", n); 
ptr = (int*)calloc(n, sizeof(int)); 
if (ptr == NULL) { 
printf("Memory not allocated.\n"); 
exit(0); 
} 
else { 
printf("Memory successfully allocated using calloc.\n"); 
for (i = 0; i < n; ++i) { 
ptr[i] = i + 1; 
} 

printf("The elements of the array are: "); 
for (i = 0; i < n; ++i) { 
printf("%d, ", ptr[i]); 
} 
} 
return 0; 
}

Output:

Please enter number of elements: 3
Memory successfully allocated using calloc.
The elements of the array are: 1, 2, 3

realloc()

If memory is insufficient for malloc () or calloc (), you can reallocate memory using the realloc () function. In short, it changes the amount of memory.

Let’s look at the syntax of the realloc () function.

ptr = realloc (ptr, new size)

free ()

The memory occupied by the malloc () or calloc () functions should be freed by calling the free () function. Otherwise, it will consume memory before the program exits.

Let’s look at the syntax of the free () function.

free (ptr)

Leave a Reply

Your email address will not be published. Required fields are marked *

Shopping cart