C Dynamic Memory Allocation

Dynamic memory allocation means allocating memory for program into run time. The run time allocation for memory is done during the program execution. In dynamic memory allocation, the heap data structure is used for allocating memory. Memory is reusable when we use dynamic memory allocation. More efficient way to allocate memory dynamically than static memory allocation. We can easily free the allocate space in case of dynamic memory allocation.
Four method manages the dynamic memory allocation in C programming language:

  1. Malloc()
  2. Calloc()
  3. Realloc()
  4. Free()

Note: Library uses for memory management function is <stdlib.h>



Malloc() Method

Malloc stands for memory allocation. Malloc() allocates memory into single block with fixed or specific size. It assigns the value into pointer type variable. Malloc() method initialize with garbage in all the value.

Syntax

  1. ptr = (castType*) malloc(size);

Example 1

  1. ptr = (int*) malloc(400 * sizeof(int));

The sizeof() operator returns the data type Size. In the above example, int size is 4 bytes, the statement will allocate 1600 (400*4) bytes of memory. The first byte of memory address assign into pointer ptr.

ptr = 1600 bytes(Single blocks)

Example 2

  1. ptr = (float*) malloc(100 * sizeof(float));

In the above example, float size is 4 bytes, the statement will allocate 400 (100*4) bytes of memory.The first byte of memory address assign into pointer ptr.

ptr = 400 bytes(single blocks)

Note: The NULL pointer returns when memory allocation fail, insufficient or memory cannot be allocated.

Here is a program example of dynamic memory allocation using malloc() method.

Example:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4. int *ptr
  5. int a, b;
  6. a = 10;
  7. printf("Enter number : %d\n", a);
  8. // Dynamically allocate memory using malloc()
  9. ptr = (int*)malloc(a * sizeof(int));
  10. if (ptr == NULL) {
  11. printf ("Memory is not allocated.\n");
  12. exit(0);
  13. }
  14. else {
  15. printf ("Successfully allocated memory using malloc.\n");
  16. for(b = 0; b < a; ++b) {
  17. ptr[b] = b + 1;
  18. }
  19. printf("The elements of the array are: ");
  20. for(b = 0; b < a; ++b) {
  21. printf ("%d", ptr[b]);
  22. }
  23. }
  24. return 0;
  25. }

Output:

Enter number : 10
Successfully allocated memory using malloc.
The elements of the array are: 1 2 3 4 5 6 7 8 9 10


Calloc() Method

Calloc stands for contiguous allocation. Calloc() allocates memory into multiple block of specific data type. It assigns the value into pointer type variable. Calloc() method initialize with zero(0) in all the value.

Syntax

  1. ptr = (cast-type*)calloc(n, data_type_size);

Example 1

  1. ptr = (int*) calloc(5 , sizeof(int));

The sizeof() operator returns the data type Size. In the above example, int size is 4 bytes, the statement will allocate 5 blocks and each block contains 4 bytes of memory. The first byte of memory address assign into pointer ptr.

The representation of ptr in example 1:

4 bytes (block no-1) 4 bytes (block no-2) 4 bytes (block no-3) 4 bytes (block no-4) 4 bytes (block no-5)

Example 2

  1. ptr = (float*) calloc(7 * sizeof(float));

In the above example, float size is 4 bytes, the statement will allocate 7 blocks and each block size is 4 bytes of memory. The first byte of memory address assign into pointer ptr.

The representation of ptr in example 2:

4 bytes (block no-1) 4 bytes (block no-2) 4 bytes (block no-3) 4 bytes (block no-4) 4 bytes (block no-5) 4 bytes (block no-6) 4 bytes (block no-7)

Here is a program example of dynamic memory allocation using calloc() method.

Example:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main() {
  4. int *ptr
  5. int a, b;
  6. a = 10;
  7. printf("Enter number : %d\n", a);
  8. // Dynamically allocate memory using calloc()
  9. ptr = (int*)calloc(a, sizeof(int));
  10. if (ptr == NULL) {
  11. printf ("Memory is not allocated.\n");
  12. exit(0);
  13. }
  14. else {
  15. printf ("Successfully allocated memory using malloc.\n");
  16. for(b = 0; b < a; ++b) {
  17. ptr[b] = b + 1;
  18. }
  19. printf("The elements of the array are: ");
  20. for(b = 0; b < a; ++b) {
  21. printf ("%d", ptr[b]);
  22. }
  23. }
  24. return 0;
  25. }

Output:

Enter number : 10
Successfully allocated memory using calloc.
The elements of the array are: 1 2 3 4 5 6 7 8 9 10

Note: Realloc() and Free() methods are described in the next tutorial.