Memory Allocation.
There are different types of dynamic memory allocation. These include.
a. The Malloc() function
b. Calloc () function
c. Relloc() function
d. Free() function
For this blog, I would focus on the first two that's Malloc and Calloc functions.
There are many cases when you do not know the exact size of arrays used in your program until much later when your program is being executed. You can specify the size of an array in advance but the array can be too small or too big if the number of data items you want to put into arrays changes dramatically at runtime.
This is where the dynamic memory allocation comes in.
With these functions, you can allocate or reallocate whilst your program is running.
Now let's vent into this memory allocation.
A. The Malloc() Functions
You can use the malloc function to allocate a specified size of memory space. The syntax for the malloc function is
Void *malloc(size-t size) ;
Here the size indicates the number of bytes of storage to allocate. The malloc function returns a void pointer.
If the malloc function fails to allocate a piece of memory space, it returns a null pointer, Normally, this happens when there is not enough memory, therefore you should always check the return pointer from malloc before you use it.
By now, you have at least grasped something about Malloc function, now let's look at this example.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int n, i,*ptr;
printf("enter total no of values:");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
printf("\nenter the value :");
for (i=0; i<n;i++)
{
scanf("%d",(ptr+i));
}
printf("\n the entered values are:");
for(i=0;i<n;i++)
{
printf("%d", *(ptr+i));
}
free(ptr);
return 0;
}
When you try this example, it would give you the entered value and that's malloc functions.
Note: Because memory is a limited resource, you should allocate an exactly sized piece of memory right before you need it and release it as soon as you are through using it.
B. The Calloc () Function
Besides the malloc() function, you can also use the Calloc function to allocate memory storage dynamically. The difference between the two functions are that the latter takes two arguments and that the memory space allocate by Calloc is always initialized to 0. There is no such thing guarantee that the memory space allocated by malloc is initialized to 0.
The syntax for the Calloc function is
Void *calloc(size-t num, size-t size)
You should also know that in Calloc, casting is not required.
In conclusion, both Malloc and Calloc are important tools for managing memory dynamically in C programs. Whatever function you choose depends on you. If you need the memory to initialize to Zero, you use Calloc, if not you can use Malloc.
Subscribe to my newsletter
Read articles from binamin binka directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
binamin binka
binamin binka
I'm Binamin Abdi, Developer From Kenya I Am A Passionate Technical Writer weaving words to simplify the complex. Transforming intricate concepts into reader-friendly content. Join me on a journey through technology's ever-evolving landscape.