C Programming Tutorial
About Lesson

C Memory Allocation

In this tutorial, we will learn about dynamically memory allocation using standard library functions: malloc(), calloc(), free() and realloc() in C programming .


Note:

As we have already learn that the size of an array cannot be changed once it is declared.

Sometimes a issue is occurs during the execution of when the size of the array we declared may be insufficient. To solve this issue, we can allocate memory manually during run-time.


Dynamic Memory Allocation

The process of allocating memory during run-time is known as dynamic memory allocation.

To allocate memory dynamically, we have provided with four function in C programming :.

  • malloc()
  • calloc()
  • realloc()
  • free()

Note : These functions are defined in the <stdlib.h> header file.


malloc()

The malloc() function is used to dynamically allocate a single large block of memory with the specified size. It returns a null pointer pointing to the memory location.

The returned pointer is usually of void type.

Note : “malloc()” stands for memory allocation.

 

Syntax :

ptr = (CastType*) malloc(ByteSize);

calloc()

The calloc() function is used to dynamically allocate the specified number of blocks of memory of the specified type. It initializes each block with a default value (0).

It return NULL if memory is not sufficient.

Note : “calloc()” stands for contiguous allocation.

 

Syntax

ptr = (CastType*)calloc(number, ByteSize);

realloc()

The realloc() function is used to dynamically change the memory allocation of a previously allocated memory i.e. we can dynamically re-allocate memory when the previously allocated memory .

It changes the memory size.

Note : “realloc()” stands for re allocation.

 

Syntax

ptr = realloc(ptr, NewSize);

Here, we have to provide two parameter first the pointer’s identifier or name and the new size where ptr is reallocated with a new size ‘NewSize’.


free()

The free() function is used to release the memory consumed by malloc() and calloc() functions.

Syntax

free(ptr);

This statement frees the space allocated in the memory pointed by ptr.


Example : malloc() and free()

// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h>
int main(){
  int *ptr, a, sum = 0;
  printf("Enter Number of elements : ");
  scanf("%d", &a);
  ptr = (int*)malloc(a*sizeof(int));//memory allocated using malloc
  if(ptr == NULL) {
    printf("Unable to allocate to memory");
    exit(0);
  }
  printf("Enter elements of array : ");
  for(int i=0; i<a; i++) {
    scanf("%d", ptr+i);
    sum += *(ptr+i);
  }
  printf("Sum = %d", sum);
  free(ptr);
  return 0;
}

Output

Enter Number of elements : 4
Enter elements of array : 1
2
3
4
Sum = 10

Example : calloc() and free()

// Program to calculate the sum of n numbers entered by the user
#include <stdio.h>
#include <stdlib.h>
int main(){
  int *ptr, a, sum = 0;
  printf("Enter Number of elements : ");
  scanf("%d", &a);
  ptr = (int*)calloc(a ,sizeof(int));//memory allocated using calloc
  if(ptr == NULL) {
    printf("Unable to allocate to memory");
    exit(0);
  }
  printf("Enter elements of array : ");
  for(int i=0; i<a; i++) {
    scanf("%d", ptr+i);
    sum += *(ptr+i);
  }
  printf("Sum = %d", sum);
  free(ptr);
  return 0;
}

Output

Enter Number of elements : 3
Enter elements of array : 1
2
3
Sum = 6

Example : realloc()

// Program to print numbers
#include <stdio.h>
#include <stdlib.h>
int main(){
  int *ptr, a, i, sum = 0;
  printf("Enter Number of elements : ");
  scanf("%d", &a);
  ptr = (int*)calloc(a ,sizeof(int));//memory allocated using calloc
  if(ptr == NULL) {
    printf("Unable to allocate to memory");
    exit(0);
  }
else {
printf("Memory allocation successed");
for(int i=0; i<a; i++) {
ptr[i] = i + 1;
}
}
printf("\nThe elements of the array are: ");
for (i = 0; i < a; i++) {
printf("%d, ", ptr[i]);
}
a = 10;
printf("\nEnter the new size of the array: %d\n", a);
ptr = realloc(ptr, a * sizeof(int));
printf("Memory successfully re-allocated using realloc.\n");
for (i = 5; i < a; i++) {
ptr[i] = i + 1;
}
printf("The elements of the array are: ");
for (i = 0; i < a; i++) {
printf("%d, ", ptr[i]);
}
free(ptr);
return 0;
}

Output

Enter Number of elements : 5
Memory allocation successed
The elements of the array are: 1, 2, 3, 4, 5,
Enter the new size of the array: 10
Memory successfully re-allocated using realloc.
The elements of the array are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,

 

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!