C - Dynamic Memory Allocation - calloc function

C Programming

Share

In this tutorial we will learn about calloc function to dynamically allocate memory in C programming language.

The calloc function

We use the calloc function to allocate memory at run time for derived data types like arrays and structures.

Using calloc function we can allocate multiple blocks of memory each of the same size and all the bytes will be set to 0. This is different from the malloc function from the previous tutorial which is used to allocate single block of memory space.

Calloc syntax

Following is the syntax of the calloc function to allocate memory dynamically.

ptr = (cast_type *) calloc (n, element_size);

Where, ptr is a pointer of type cast_type.

n is the total block of contiguous spaces, each of size element_size to be allocated using the calloc function.

Calloc example

In the following example we are allocating memory space for 3 student structure variable.

// student structure
struct student {
  char id[10];
  char firstname[64];
  char lastname[64];
  int score;
};

// new type
typedef struct student candidate;

// student structure pointer
candidate *sptr;

// variables
int no_of_students = 3;

// allocate memory blocks
sptr = (candidate *) calloc (no_of_students, sizeof(candidate));

In the above code we are allocating 3 blocks of memory space each of size candidate structure i.e., 140 bytes.

Member Data Type Size
id char 10 bytes
firstname char 64 bytes
lastname char 64 bytes
score int 2 bytes

Write a program in C to dynamically allocate memory using calloc function for a structure

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    
  // student structure
  struct student {
    char id[10];
    char firstname[64];
    char lastname[64];
    int score;
  };
  
  // new type
  typedef struct student candidate;
  
  // student structure pointer
  candidate *sptr;
  candidate *tmp;
  
  // variables
  int no_of_students = 3;
  int i;
  
  // allocate memory blocks
  sptr = (candidate *) calloc (no_of_students, sizeof(candidate));
  
  // get student details
  for(i = 0, tmp = sptr; i < no_of_students; i++, tmp++) {
    printf("Enter detail of student #%d\n", (i+1));
    printf("ID: ");
    scanf("%s", tmp->id);
    printf("First Name: ");
    scanf("%s", tmp->firstname);
    printf("Last Name: ");
    scanf("%s", tmp->lastname);
    printf("Score: ");
    scanf("%d", &tmp->score);
  }
  
  // display student details
  printf("\n\nFollowing are the student details:\n\n");
  for(i = 0, tmp = sptr; i < no_of_students; i++, tmp++) {
    printf("Detail of student #%d\n", (i+1));
    printf("ID: %s\n", tmp->id);
    printf("First Name: %s\n", tmp->firstname);
    printf("Last Name: %s\n", tmp->lastname);
    printf("Score: %d\n", tmp->score);
  }
  
  // free memory location
  free(sptr);

  return 0;
}

Output:

Enter detail of student #1
ID: s01
First Name: Yusuf
Last Name: Shakeel
Score: 8
Enter detail of student #2
ID: s02 
First Name: Jane
Last Name: Doe
Score: 9
Enter detail of student #3
ID: s03
First Name: John
Last Name: Doe
Score: 7


Following are the student details:

Detail of student #1
ID: s01
First Name: Yusuf
Last Name: Shakeel
Score: 8
Detail of student #2
ID: s02
First Name: Jane
Last Name: Doe
Score: 9
Detail of student #3
ID: s03
First Name: John
Last Name: Doe
Score: 7

We can represent the allocated memory as follows.



So, the three structures each of size 140 bytes are allocated memory location from address 1000 to 1139, 2000 to 2139 and 3000 to 3139.

The sptr structure pointer points at these memory locations.

We are using a temporary tmp structure pointer to access these allocated memory blocks.

Share