Dynamic Memory Allocation In C

Dynamic memory allocation is the process of allocation of memory space at the run time. We use the concept of dynamic memory allocation to reduce the wastage of memory, and it is the optimal way of memory allocation. Dynamic memory is managed and served with pointers that point to the newly allocated memory space in an area which we call the heap.

Now you can create and destroy an array of elements dynamically at runtime without any problems. To sum up, the automatic memory management uses the stack, and the C Dynamic Memory Allocation uses the heap.

Dynamic memory allocation in c language is possible by 4 functions of <stdlib.h> header file.

FunctionPurpose
malloc()Allocates the memory of requested size and returns the pointer to the first byte of allocated space.
calloc()Allocates the space for elements of an array.
realloc()It is used to modify the size of previously allocated memory space.
Free()Free or empties the previously allocated memory space.

malloc() function in C :

The C malloc() function stands for memory allocation. It is a function which is used to allocate a block of memory dynamically. It reserves memory space of specified size and returns the null pointer pointing to the memory location. The pointer returned is usually of type void. It means that we can assign C malloc() function to any pointer.

The syntax of malloc() function is given below:

ptr=(cast-type*)malloc(byte-size)  

Let’s see the example of malloc() function.

#include <stdlib.h>
int main(){
int *ptr;
ptr = malloc(15 * sizeof(*ptr)); /* a block of 15 integers */
    if (ptr != NULL) {
      *(ptr + 5) = 480; /* assign 480 to sixth integer */
      printf("Value of the 6th integer is %d",*(ptr + 5));
    }
}

Output :
Value of the 6th integer is 480

calloc() function in C :

The C calloc() function stands for contiguous allocation. This function is used to allocate multiple blocks of memory. It is a dynamic memory allocation function which is used to allocate the memory to complex data structures such as arrays and structures.

Malloc() function is used to allocate a single block of memory space while the calloc() in C is used to allocate multiple blocks of memory space. Each block allocated by the calloc() function is of the same size.

The syntax of calloc() function is given below:

ptr=(cast-type*)calloc(number, byte-size)

Let’s see the example of calloc() function.

#include <stdio.h>
    int main() {
        int i, * ptr, sum = 0;
        ptr = calloc(10, sizeof(int));
        if (ptr == NULL) {
            printf("Error! memory not allocated.");
            exit(0);
        }
        printf("Building and calculating the sequence sum of the first 10 terms \ n ");
        for (i = 0; i < 10; ++i) { * (ptr + i) = i;
            sum += * (ptr + i);
        }
        printf("Sum = %d", sum);
        free(ptr);
        return 0;
    }

Output :
Building and calculating the sequence sum of the first 10 terms
Sum = 45

realloc() function in C :

Using the C realloc() function, you can add more memory size to already allocated memory. It expands the current block while leaving the original content as it is. realloc() in C stands for reallocation of memory.

realloc() can also be used to reduce the size of the previously allocated memory.

Let’s see the syntax of realloc() function.

ptr=realloc(ptr, new-size)

Let’s see the example of realloc() function.

#include <stdio.h>
int main () {
   char *ptr;
   ptr = (char *) malloc(10);
   strcpy(ptr, "Programming");
   printf(" %s,  Address = %u\n", ptr, ptr);

   ptr = (char *) realloc(ptr, 20); //ptr is reallocated with new size
   strcat(ptr, " In 'C'");
   printf(" %s,  Address = %u\n", ptr, ptr);
   free(ptr);
   return 0;
}

free() function in C :

The memory occupied by malloc() or calloc() functions must be released by calling free() function. Otherwise, it will consume memory until program exit.

Let’s see the syntax of free() function.

free(ptr)

2 thoughts on “Dynamic Memory Allocation In C”

Leave a Comment

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

// Sticky ads
Your Poster