× Home
Next Lec → ← Previous Lec

Dynamic Memory allocation: Malloc Calloc Realloc & Free()

This is important topic

Dynamic memory allocation : Recap

Functions for Dynamic memroy allocation in C

MALLOC()

  • malloc() stands for memory allocation
  • It reserves a block of memory with the given amount of bytes.
  • The return value is a void pointer to the allocated space
  • Therefore the void pointer needs to be casted to the appropriate type as per the requirements
  • However, if the space is insufficient, allocation of memory fails and it returns a NULL pointer.
  • All the values at allocated memroy are intitialized to garbage values
    • Syntax:
    • ptr = (ptr-type*) malloc(size_in_bytes);
      • int *ptr;
      • ptr = (int*) malloc(3* sizeof(int));
      • //we are using sizeof() because in different architecture its value changes

CALLOC()

  • calloc() stands for contiguous allocation
  • It reserves n blocks of memory with the given amount of bytes.
  • The return value is a void pointer to the allocated space
  • Therefore the void pointer needs to be casted to the appropriate type as per the requirements
  • However, if the space is insufficient, allocation of memory fails and it returns a NULL pointer.
  • All the values at allocated memory are initialzed to 0
  • syntax:
    • ptr = (ptr-type*) calloc(n, size_in_bytes);

REALLOC()

  • realloc() stands for reallocation
  • If the dynamically allocated memory is unsufficient we can change the size of previously allocated memory using realloc() function
  • Syntax:
    • ptr = (ptr-type*) realloc(ptr, new_size_in_bytes);

FREE()

  • free() is used to free the allocated memory
  • If the dynamicaly allocated memory is not required anymore, we can free it using free function
  • This will free the memory being used by the program in the heap
  • Syntax:
    • free(ptr);

Code Examples

                        
                            
                         #include <stdio.h>
                         #include <stdlib.h> //calloc malloc realloc and free are defined inside this header file
                        
                         int main()
                         {
                            // use of malloc
                            int *ptr;
                            int n;
                            printf("Enter the size of teh array you want to create :");
                            scanf("%d",&n);
                            ptr = (int *)malloc(n * sizeof(int)); // create dynamic array of 10 size
                            for (int i = 0; i < n; i++)
                            {
                                printf("Enter the value no %d of this :", i);
                                scanf("%d", &ptr[i]);
                            }
                            
                            for (int i = 0; i < n; i++)
                            {
                                printf("The value %d of this array is %d\n", i, ptr[i]);
                            }
                            
                            return 0;
                         }
                        
                    
                        

                         #include <stdio.h>
                         #include <stdlib.h>
                         int main()
                         {
                            int *ptr;
                            int n;
                            ptr = (int *)calloc(n, sizeof(int));
                            printf("Enter the size of teh array you want to create :");
                            scanf("%d", &n);
                            for (int i = 0; i < n; i++)
                            {
                                printf("Enter the value no %d of this :", i);
                                scanf("%d", &ptr[i]);
                                //in calloc if we don't initailize it with any value it assigns 0
                                //so initializing is a extra task here that's why malloc is prefered
                            }
                            
                            for (int i = 0; i < n; i++)
                            {
                                printf("The value %d of this array is %d\n", i, ptr[i]);
                            }
                            
                            return 0;
                         }
                        
                    
                        

                        #include <stdio.h>
                         #include <stdlib.h>
                         int main()
                         {
                             // this all was allocated using calloc
                             int *ptr;
                             int n;
                             ptr = (int *)calloc(n, sizeof(int));
                             printf("Enter the size of teh array you want to create :");
                             scanf("%d", &n);
                             for (int i = 0; i < n; i++)
                             {
                                printf("Enter the value no %d of this :", i);
                                scanf("%d", &ptr[i]);
                                // in calloc if we don't initailize it with any value it assigns 0
                                // so initializing is a extra task here that's why malloc is 
prefered } for (int i = 0; i < n; i++) { printf("The value %d of this array is %d\n", i, ptr[i]); } // from here we will allocate memory using realloc ptr = (int *)realloc(ptr,n*sizeof(int)); printf("Enter the size of the new array you want to create :"); scanf("%d", &n); for (int i = 0; i < n; i++) { printf("Enter the new value no %d of this :", i); scanf("%d", &ptr[i]); } for (int i = 0; i < n; i++) { printf("The new value %d of this array is %d\n", i, ptr[i]); } return 0; }
                        

                        #include <stdio.h>                        
                         #include <stdlib.h> //calloc malloc realloc and free are defined inside this header file
                        
                         int main()
                         {
                            // use of malloc
                            int *ptr;
                            int n;
                            ptr = (int *)malloc(n * sizeof(int)); // create dynamic array of 10 size
                            printf("Enter the size of teh array you want to create :");
                            scanf("%d",&n);
                            for (int i = 0; i < n; i++)
                            {
                                printf("Enter the value no %d of this :", i);
                                scanf("%d", &ptr[i]);
                            }
                            
                            for (int i = 0; i < n; i++)
                            {
                                printf("The value %d of this array is %d\n", i, ptr[i]);
                            }
                            free(ptr);
                            return 0;
                        }