Calloc in C
×


Calloc in C

28

 calloc stands for "contiguous allocation."

 It is a memory allocation function in C used to allocate a specified number of blocks of memory, each of a specified size.

 Unlike malloc, calloc initializes the allocated memory to zero.

 This makes it particularly useful for initializing arrays and structures.

Key Features

 Memory initialization: unlike malloc, calloc initializes the allocated memory to zero.

 Size Calculation: It takes two arguments - the number of elements and the size of each element, automatically calculating the total memory required.

 Return Type: Returns a pointer to the first byte of the allocated memory block.

 The calloc function is used to allocate memory for an array of elements.


Syntax of calloc in C:

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

 ptr: Pointer to the block of memory.

 castType: Type of data to be allocated.

 n: Number of elements.

 size: Size of each element.

 calloc initializes the allocated memory to zero.

 calloc gives back a pointer to the starting point of the allocated memory.


Programs Using calloc:

Program to Allocate and Print Array Using calloc

// Program to Allocate and print Array using calloc.
#include<stdio.h> 
#include<stdlib.h> 

int main() {
    int *arr;
    int n, i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    arr = (int*) calloc(n, sizeof(int));

    if(arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    printf("Enter %d elements:\n", n);
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Elements in array are:\n");
    for(i = 0; i < n; i++) {
        printf("%d\n", arr[i]);
    }

    free(arr);
    return 0;
}
		
	

Output:

Enter number of elements: 2
Enter 2 elements:
3
5
Elements in array are:
3
5

Program to Allocate 2D Array Using calloc

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

int main() {
    int **matrix;
    int rows, cols, i, j;

    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

    matrix = (int**) calloc(rows, sizeof(int*));
    for(i = 0; i < rows; i++) {
        matrix[i] = (int*) calloc(cols, sizeof(int));
    }

    printf("Enter elements of the matrix:\n");
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    printf("Matrix is:\n");
    for(i = 0; i < rows; i++) {
        for(j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    // Free memory
    for(i = 0; i < rows; i++) {
        free(matrix[i]);
    }
    free(matrix);

    return 0;
}
		
	

Output:

Enter number of rows: 3
Enter number of columns: 2
Enter elements of the matrix:
2
4
2
5
5
6
Matrix is:
2 4 
2 5 
5 6 

Program to Allocate and Initialize Memory Using calloc

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

int main() {
    int *arr;
    int n, i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    arr = (int*) calloc(n, sizeof(int));

    if(arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    // Initialize array elements
    for(i = 0; i < n; i++) {
        arr[i] = i + 1;
    }

    printf("Elements in array are:\n");
    for(i = 0; i < n; i++) {
        printf("%d\n", arr[i]);
    }

    free(arr);
    return 0;
}

Output:

Enter number of elements: 3
Elements in array are:
1
2
3

In these programs:

Memory is allocated using calloc.

Arrays are initialized either by user input or sequentially.

Memory is freed using free after its use to prevent memory leaks.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments