Dynamic Memory Allocation in C Programming
Dynamic Memory Allocation in C Programming
C programming
CategoriesC programming9 mins readJune 26, 2018
We have discussed in one of previous article about Compile time and Runtime
memory allocation. C supports two ways to allocate memory for
the variables used in the program. Here in this article I will explain about
Dynamic Memory Allocation in C programming language.
Before getting our hands dirty with managing memory manually. Let us first
learn the need for dynamic memory allocation in C.
malloc() function
malloc() allocates N bytes in memory and return pointer to allocated
memory. The returned pointer contains link/handle to the allocated memory.
Using that you can access the memory allocated.
Syntax
void * malloc(number_of_bytes);
Copy
It returns void pointer (generic pointer). Which means we can
easily typecast it to any other pointer types.
It accepts an integer number_of_bytes, i.e. total bytes to allocate in
memory.
Note: malloc() returns NULL pointer on failure.
Example
int N = 10; // Number of bytes to allocate
int *ptr; // Pointer variable to store address
/**
* C program to demonstrate malloc() and free() function.
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, max;
int *ptr;
return 0;
}
Copy
Note: I will discuss about free() function at the end of this article.
Output:
calloc() function
calloc() function allocates memory contiguously. It allocates multiple
memory blocks and initializes all blocks with 0 (NULL).
Syntax
void* calloc(number_of_blocks, number_of_bytes);
Copy
Here,
Example
Similar to malloc() function, we can also use calloc() to reserve memory
for N integers.
int *ptr;
ptr = (int *) calloc(N, sizeof(int));
Copy
Here, all memory blocks are initialized with 0.
Note: Below program is same as malloc() function program. Only the way to
allocate memory is different
for calloc() i.e. ptr = (int*) calloc(max, sizeof(int));.
/**
* C program to demonstrate calloc() and free() function.
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, max;
int *ptr;
return 0;
}
Copy
Output:
realloc() function
When working with huge data and if the allocated memory is not sufficient to
store data. In that case, we need to alter/update the size of an existing
allocated memory blocks (which has been created by
either malloc() or calloc()).
Syntax
void* realloc(ptr, updated_memory_size);
Copy
Similar to all other functions for Dynamic Memory Allocation in C,
it returns void pointer. Which points to the address of existing or
newly allocated memory.
ptr is a pointer to memory block of previously allocated memory.
updated_memory_size is new (existing + new) size of the memory
block.
Example
// Original memory blocks allocation
int N = 10;
int *ptr;
ptr = (int *) malloc(N * sizeof(int));
int main()
{
int i, max, newSize;
int *ptr;
// Reallocate memory
printf("\nEnter new size of the array: ");
scanf("%d", &newSize);
ptr = (int *) realloc(ptr, (newSize * sizeof(int)));
return 0;
}
Copy
Output:
Enter 2 elements: 60 70
free() function
Till now we learned to allocate and reallocate memory in C. But moreover
allocation, how to clear allocated memory is important.
The free() function clears the pointer (assigns NULL to the pointer) to clear
the dynamically allocated memory. If pointer contains NULL, then free() does
nothing (because pointer will not be pointing at any memory addresses). If it
contains any address of dynamically allocated memory, free() will clear
pointer by assigning NULL.
Syntax
free(ptr);
Copy
The function accepts a void pointer ptr. It points to previously allocated
memory using any of Dynamic Memory Allocation functions in C.
Example
int N = 10;
int *ptr;
Happy coding