malloc() Function- What is the difference between "calloc(...)" and "malloc(...)"?

28 Dec 2022 Balmiki Mandal 0 C Programming

malloc() Function: Difference between calloc(...) and malloc(...)

Introduction

When it comes to dynamic memory allocation in C programming, two commonly used functions are malloc() and calloc(). While both serve the purpose of allocating memory dynamically, they have some crucial differences that developers should be aware of. This page will provide a comprehensive comparison between calloc(...) and malloc(...) to help you make informed decisions in your programming endeavors.

malloc()

Overview

  • malloc() is a standard library function in C used for dynamic memory allocation.
  • It allocates a block of memory of a specified size and returns a pointer to the beginning of that block.
  • The memory allocated by malloc() is uninitialized, which means it may contain garbage values.

Usage

void* malloc(size_t size);

 

Example

int* ptr = (int*)malloc(5 * sizeof(int));
if (ptr == NULL) {
    // Allocation failed
    exit(1);
}

calloc()

Overview

  • calloc() is also a standard library function in C used for dynamic memory allocation.
  • It allocates a block of memory of specified size and returns a pointer to the beginning of that block.
  • The key difference is that calloc() initializes the allocated memory to zero.

Usage

void* calloc(size_t num, size_t size);

 

Example

int* ptr = (int*)calloc(5, sizeof(int));
if (ptr == NULL) {
    // Allocation failed
    exit(1);
}

 

Key Differences

Initialization

  • malloc(): Does not initialize the allocated memory. It may contain garbage values.
  • calloc(): Initializes the allocated memory to zero.

Arguments

  • malloc(): Takes one argument - the size of the memory block in bytes.
  • calloc(): Takes two arguments - the number of elements to allocate and the size of each element in bytes.

Performance

  • calloc() may be slightly slower than malloc() due to the extra step of initializing memory.

Use Cases

  • Use malloc() when you need to allocate uninitialized memory and are concerned about performance.
  • Use calloc() when you need to allocate zero-initialized memory, for example, when working with arrays or data structures.

Example 01 that demonstrates the difference between malloc() and calloc():

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

int main() {
    int* ptr1;
    int* ptr2;
    ptr1 = (int*)malloc(5 * sizeof(int));
    ptr2 = (int*)calloc(5, sizeof(int));
    
    printf("ptr1 = ");
    for(int i=0; i<5; i++) {
        printf("%d ", ptr1[i]);
    }
    
    printf("\nptr2 = ");
    for(int i=0; i<5; i++) {
        printf("%d ", ptr2[i]);
    }
    
    return 0;
}

Output:

ptr1 = 0 0 0 0 0 
ptr2 = 0 0 0 0 0 

As we can see from the output, the memory allocated by malloc() contains garbage values, whereas the memory allocated by calloc() is initialized to zero.

Conclusion

Choosing between malloc() and calloc() depends on your specific use case. Understanding their differences allows you to make the right choice for efficient and error-free memory allocation in your C programs.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.