Skip to main content

Difference Between malloc() and calloc() with Examples

 Pre-requisite: Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()

The name malloc and calloc() are library functions that allocate memory dynamically. It means that memory is allocated during runtime(execution of the program) from the heap segment.

  • Initialization: malloc() allocates memory block of given size (in bytes) and returns a pointer to the beginning of the block. malloc() doesn’t initialize the allocated memory. If we try to access the content of memory block(before initializing) then we’ll get segmentation fault error(or maybe garbage values)
void* malloc(size_t size); 

calloc() allocates the memory and also initializes the allocated memory block to zero. If we try to access the content of these blocks then we’ll get 0.

void* calloc(size_t num, size_t size); 

  • Number of arguments: Unlike malloc(), calloc() takes two arguments:
    1) Number of blocks to be allocated.
    2) Size of each block.
  • Return Value: After successful allocation in malloc() and calloc(), a pointer to the block of memory is returned otherwise NULL value is returned which indicates the failure of allocation.
  • For instance, If we want to allocate memory for array of 5 integers, see the following program:-

    // C program to demonstrate the use of calloc() 

    // and malloc() 

    #include <stdio.h> 

    #include <stdlib.h> 


    int main() 

    int* arr; 


    // malloc() allocate the memory for 5 integers 

    // containing garbage values 

    arr = (int*)malloc(5 * sizeof(int)); // 5*4bytes = 20 bytes 


    // Deallocates memory previously allocated by malloc() function 

    free(arr); 


    // calloc() allocate the memory for 5 integers and 

    // set 0 to all of them 

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


    // Deallocates memory previously allocated by calloc() function 

    free(arr); 


    return (0); 

    ptr = malloc(size); 
    memset(ptr, 0, size); 
    Note: It would be better to use malloc over calloc, unless we want the zero-initialization because malloc is faster than calloc. So if we just want to copy some stuff or do something that doesn’t require filling of the blocks with zeros, then malloc would be a better choice.

Comments

Popular posts from this blog

System Analysis and Design Elias M. Awad (Unit - 1)

Systems development is systematic process which includes phases such as planning, analysis, design, deployment, and maintenance. example :- Computer System , Business System , Hotel , Library , College. Audience This tutorial will help budding software professionals to understand how a system is designed in a systematic and phased manner, starting from requirement analysis to system implementation and maintenance. Prerequisites This tutorial is designed for absolute beginners and hence there are no prerequisites as such, however it is assumed that the reader is familiar with the fundamentals of computers. System   Systems development is systematic process which includes phases such as planning, analysis, design, deployment, and maintenance. Here, in this tutorial, we will primarily focus on − Systems analysis Systems design Systems Analysis It is a process of collecting and interpreting facts, identifying the problems, and decomposition of a system into its components. System analy...

Doubly Linked List - Data Structure

                           Doubly Linked List A doubly linked list is a  linked list data structure that includes a link back to the previous node in each node in the structure . This is contrasted with a singly linked list where each node only has a link to the next node in the list. List is a variation of Linked list in which navigation is possible in both ways, either forward and backward easily as compared to Single Linked List. Following are the important terms to understand the concept of doubly linked list. Link  − Each link of a linked list can store a data called an element. Next  − Each link of a linked list contains a link to the next link called Next. Prev  − Each link of a linked list contains a link to the previous link called Prev. LinkedList  − A Linked List contains the connection link to the first link called First and to the last link called Last. Doubly Linked List Represen...

Information Gathering Tool (System Analysis and Design)

  Information gathering tools... Introduction Information Gathering is a very key part of the feasibility analysis process. Information gathering is both an art and a science. It is a science because it requires a proper methodology and tools in order to be effective. It is an art too, because it requires a sort of mental dexterity to achieve the best results. In this article we will explore the various tools available for it, and which tool would be best used depending on the situation. Information Gathering Tools There is no standard procedures defined when it comes to the gathering of information. However, an important rule that must be followed is the following: information must be acquired accurately and methodically, under the right conditions and with minimum interruption to the individual from whom the information is sought. 1. Review of Procedural Forms These are a very good starting point for gathering information. Procedural manuals can give a good picture of the system ...