C calloc() Function Complete Tutorial

In this section, we will learn what the calloc() function is and how to use it in C.

Note: we’re assuming you already familiar with the C `malloc() function`.

Allocate Memory in C: calloc() Function

The `calloc()` function similar to the `malloc` function is used to allocate dynamic memory space to variables.

The prototype of this function exists in the `stdlib.h` header file and so in order to use the function, we need to include the header file as well.

C calloc() Function Syntax:

Here’s the prototype of the `calloc` function:

void *calloc(size_t nitems, size_t size)

C calloc() Function Parameters:

This function takes two arguments:

  1. The first argument declares the number of blocks of memory that should be set aside.
  2. The second argument is the size of each block. For example, if we want 10 blocks of memory in order to store 10 integer numbers, then the size of each block is equal to either 4 or 8 bytes depending on the underlying system. (Use the value returned from the call to the `sizeof` operator in order to get exact value).

C calloc() Function Return Value:

The returned value of this function is the address to the starting point of the memory space that is set aside by this function.

Note: the returned pointer of the function is a general pointer and so we need to cast it to the related type. For example, if we’re going to store integer variable in this memory space, we then need to cast the returned pointer to a pointer of type int like `(int *)`.

Example: using calloc() function in C

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

int *pointer;
int main() {

    int size;
    printf("Please enter the number of elements: \n");
    scanf("%d",&size);
    pointer = (int *) calloc(size , sizeof(int));
    for (int i = 0 ; i< size; i++){
        *(pointer+i) = i;
    }
    printf("The values stored in the array are: \n");
    for (int i = 0; i<size; i++){
        printf("%d, ",*(pointer + i));
    }

    free(pointer);
return 0;

}

Output:

Please enter the number of elements:

10

The values stored in the array are:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,

How to use calloc() function?

In this example, we’ve asked a user to enter how many elements she wants to store in the program.

The input value is 10, so we set the first argument of the `calloc` function to 10, which means there are going to be 10 blocks of memory set aside for us! Because each integer value takes 4 bytes in one system and 8 bytes in another, we used the `sizeof` operator in order to get the actual size of one integer value and then set it as the second argument to the `calloc` function so to define the length of each block. The result of this call is the memory space in bytes that we need in order to store 10 integer values.

After that, the `calloc` function returned a general pointer to the first byte of this dynamically allocated memory space and we cast it and assigned the address to the variable `pointer`.

Note:

When we create a variable of type integer or double or float, etc. the compiler takes care of creating and cleaning the memory space allocated to the variable automatically.

But when we access a memory space via the call to a function like `calloc` or `malloc`, that memory space won’t go anywhere until the end of the program and so it’s on us as developers to take care of cleaning this memory space when we’re done with it.

This is where the function named `free` comes in. You’ll learn more about this in `free function` section, but in short, this function takes one argument and that is the address returned from the `calloc` or `malloc` function.

The golden rule is that for each call to `calloc` or `malloc` function, there should be another call to the `free` function at some point in the program in order to make sure the memory space is released.

So here in our example, the last line of execution in the body of the `main` function is to call the `free` function in order to release the memory space allocated via the call to the `calloc`.

More to Read:

C malloc() Function

C free() Function

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies