Malloc() Vs Calloc() Vs Realloc() in C
Malloc(), Calloc() and Realloc():
On this page we will discuss about dynamic memory allocation using malloc function, contiguous memory allocation using calloc function and realloction of memory using realloc function in C programming language.
															Definitions:
Malloc()
- The memory allocation function, or malloc(), allocates a block of memory in a dynamic manner.
 - It returns the null pointer, which corresponds to the memory address, and reserves the memory space for the provided size.
 - A memory block of specified size is allocate at run time by malloc.
 - A pointer of void type is returned by malloc function which can be convert into pointer of any type.
 
Calloc()
- The dynamic memory allocation in C can be done by using the calloc function.
 - The Calloc function makes it possible to allocate memory in many blocks of the same size.
 - It is defined in the stdlib.h header file.
 - Every memory block was initially set to zero by the calloc() function.
 
Realloc()
- The C library’s realloc() function allows you to increase the size of memory blocks that have already been allocated.
 - Realloc is a function in C that allows you to increase the size of existing memory blocks without changing their content.
 - The realloc() function aids in shrinking memory that has previously been allocated using the malloc or calloc procedures.
 - The block’s contents are unaltered up to the smaller of the two sizes.
 
Important Note :
- Realloc( ) function is used to change the size of the memory block without losing the old data.
 - It is a built in function declared in stdlib.h .As the name itself suggests realloc( ) means reallocation.
 
Syntax:
void*realloc(void*ptr, newsize);
Example of Malloc():
Run
								#include<stdio.h>
#include<stdlib.h>
int main ()
{
  int n, i, *ptr, sum = 0;
  printf ("Enter number of elements = ");
  scanf ("%d", &n);
  ptr = (int *) malloc (n * sizeof (int));
  if (ptr == NULL)
    {
      printf ("Memory not allocated.");
      exit (0);
    }
  printf ("Enter elements = ");
  for (i = 0; i < n; ++i)
    {
      scanf ("%d", ptr + i);
      sum += *(ptr + i);
    }
  printf (" Total Sum = %d", sum);
  free (ptr);
  return 0;
}
Output:
Enter number of elements = 5 Enter elements = 9 8 7 6 5 Total Sum = 35
Example of Calloc():
Run
								
#include<stdio.h>
#include<stdlib.h>
int main ()
{
  int n, i, *ptr, sum = 0;
  printf ("Enter number of elements = ");
  scanf ("%d", &n);
  ptr = (int *) calloc (n, sizeof (int));
  if (ptr == NULL)
    {
      printf ("Memory not allocated");
      exit (0);
    }
  printf ("Enter elements = ");
  for (i = 0; i < n; ++i)
    {
      scanf ("%d", ptr + i);
      sum += *(ptr + i);
    }
  printf ("Total Sum = %d", sum);
  free (ptr);
  return 0;
}
Output:
Enter number of elements = 5 Enter elements = 1 2 3 4 5 Total Sum = 15
Example of Realloc():
Run
								
#include<stdio.h>
#include<stdlib.h>
int main ()
{
  int *ptr, i, n1, n2;
  printf ("Enter size = ");
  scanf ("%d", &n1);
  ptr = (int *) malloc (n1 * sizeof (int));
  printf ("Addresses of previously allocated memory =\n");
  for (i = 0; i < n1; ++i)
    printf ("%pc\n", ptr + i);
  printf ("\nEnter the new size = ");
  scanf ("%d", &n2);
  ptr = realloc (ptr, n2 * sizeof (int));
  printf ("Addresses of newly allocated memory =\n");
  for (i = 0; i < n2; ++i)
    printf ("%pc\n", ptr + i);
  free (ptr);
  return 0;
}
Output:
Enter size = 5 Addresses of previously allocated memory = 0x555601564ac0c 0x555601564ac4c 0x555601564ac8c 0x555601564accc 0x555601564ad0c Enter the new size = 6 Addresses of newly allocated memory = 0x555601564ac0c 0x555601564ac4c 0x555601564ac8c 0x555601564accc 0x555601564ad0c 0x555601564ad4c
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

								
								
								
                            
                        
Login/Signup to comment