Dynamic Memory Allocation

Dynamic Memory Allocation In C Programming Language

By using the standard library functions malloc(), calloc(), free(), and realloc in your C program, you will learn about dynamic memory allocation in C in this tutorial ().

An array, as you are aware, is a grouping of a predetermined number of values. Once an array’s size is declared, it cannot be changed.

The array you declared may occasionally be too small. You can manually allocate memory during run-time to fix this problem. In C programming, this is referred to as dynamic memory allocation.

The library functions malloc(), calloc(), realloc(), and free() are used to dynamically allocate memory. The stdlib.h header file contains the definitions of these functions.

C malloc()(Dynamic Memory Allocation In C)

Malloc is the short form for memory allocation.

The malloc() function makes a memory block with the specified number of bytes available for use. Additionally, it returns a void pointer that can be cast into any other kind of pointer.

Syntax of malloc()(Dynamic Memory Allocation In C)

ptr = (castType*) malloc(size);

Example

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

400 bytes of RAM are allocated by the previous statement. It’s because float only takes up 4 bytes. Additionally, the address of the first byte in the allocated memory is contained in the pointer ptr.

If the memory cannot be allocated, the expression returns a NULL pointer.

C calloc()(Dynamic Memory Allocation In C)

Contiguous allocation is represented by the word “calloc”.

The calloc() function allocates memory and initializes all of the bits to zero, as opposed to the malloc() function, which creates memory but does not initialize it.

Syntax of calloc()[Dynamic Memory Allocation In C]

ptr = (castType*)calloc(n, size);

Example:

ptr = (float*) calloc(25, sizeof(float));

C free()

Memory that was dynamically allocated and started with either calloc() or malloc() is not released on its own. To release the space, you must specifically use free().

Syntax of free()[Dynamic Memory Allocation In C]

free(ptr);

By saying this, the memory location referenced to by ptr has its assigned space released.

Example 1: malloc() and free()[Dynamic Memory Allocation In C]

// Program to calculate the sum of n numbers entered by the user

#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 memory cannot be allocated
  if(ptr == NULL) {
    printf("Error! memory not allocated.");
    exit(0);
  }

  printf("Enter elements: ");
  for(i = 0; i < n; ++i) {
    scanf("%d", ptr + i);
    sum += *(ptr + i);
  }

  printf("Sum = %d", sum);
  
  // deallocating the memory
  free(ptr);

  return 0;
}

Output

Enter number of elements: 3
Enter elements: 100
20
36
Sum = 156

We have dynamically allocated n number of ints in this location.

Example 2: calloc() and free()[Dynamic Memory Allocation In C]

// Program to calculate the sum of n numbers entered by the user

#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("Error! memory not allocated.");
    exit(0);
  }

  printf("Enter elements: ");
  for(i = 0; i < n; ++i) {
    scanf("%d", ptr + i);
    sum += *(ptr + i);
  }

  printf("Sum = %d", sum);
  free(ptr);
  return 0;
}

Output

Enter number of elements: 3
Enter elements: 100
20
36
Sum = 156

C realloc()[Dynamic Memory Allocation In C]

The realloc() function can be used to modify the size of previously allocated memory if the dynamically allocated memory is either insufficient or greater than what is needed.

Syntax of realloc()[Dynamic Memory Allocation In C]

ptr = realloc(ptr, x);

Here, ptr is reallocated with a new size x.

Example 3: realloc()(Dynamic Memory Allocation In C)

#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);

  // rellocating the memory
  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: 2
Addresses of previously allocated memory:
26855472
26855476
Enter the new size: 4
Addresses of newly allocated memory:
26855472
26855476
26855480
26855484

you may like:

C++ Programming Default Arguments (Parameters) with Example

Leave a Reply