• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » DS » Basic » What is the difference between “calloc(…)” and “malloc(…)”
Next →
← Prev

What is the difference between “calloc(…)” and “malloc(…)”

By Dinesh Thakur

The malloc () function is used to allocate memory and has the following prototype:

void * malloc (unsigned int num);

malloc(…) takes in only a single argument which is the memory required in bytes. The function gets the number of bytes that we allocate (a) allocates memory and returns a pointer void * allocated to the first byte. The void * pointer can be assigned to any type of pointer. If there is not enough memory to allocate the requested memory malloc () returns a null pointer. malloc(…) allocated bytes of memory and not blocks of memory like calloc(…). See an example of dynamic allocation with malloc ():

#include <stdio.h>
#include <stdlib.h> /* To use malloc () */
main (void)
{
    int *p;
    int a;
    int i;
    ... /* Determines the value to somewhere */
    p = (int *) malloc (a * sizeof (int)); /* Allocate the integers p can now be treated as a vector the positions */
    if (!p)
         {
         printf ("** Error: Insufficient Memory **");
         exit;
         }
    for (i = 0; i<a; i ++) /* p can be treated as a vector with the positions */
    p[i] = i * i;
    ...
    return 0;
}

In the example above, is to allocate enough memory to store the integers. The sizeof () operator returns the number of bytes of an integer. It is useful to know the size of type. The pointer void * malloc () returns is converted to an int * and the cast is assigned to p. The following statement tests whether the operation was successful. If not, p has a null value, which will cause !P returns true. If the operation is successful, we can use the whole allocated vector normally, for example by indexing the p [0] a p [(a-1)].

The calloc () function also serves to allocate memory, but has a prototype a little different:

void * calloc (unsigned int a, unsigned int size);

The function allocates an amount of memory equal to a * size, that is, allocates enough memory to a vector of a size objects. Returns a void * pointer to the first byte allocated. The void * pointer can be assigned to any type of pointer. If there is not enough memory to allocate memory required to calloc () function returns a null pointer. By default the block is initialized to 0. The total number of memory allocated will be (number_of_elements * size). See an example of dynamic allocation with calloc ():

#include <stdio.h> 
#include <stdlib.h> /* To use calloc () */
main (void)
{
    int *p;
    int a;
    int i;
    ... /* Determines the value to somewhere */
    p = (int *) calloc (a, sizeof (int)); /* Allocate the integers p can now be treated as a vector the positions */
    if (!p)
         {
         printf ("** Error: Insufficient Memory **");
         exit;
         }
    for (i = 0; i <a; i ++) / * p can be treated as a vector with the positions * /
    p [i] = i * i;
    ...
    return 0;
}

In the above example, memory is allocated enough to put the integers. The sizeof () operator returns the number of bytes of an integer. It is useful to know the size of type. The void * pointer that calloc () returns is converted to an int * and the cast is assigned to p. The following statement tests whether the operation was successful. If not, p has a null value, which will cause! P returns true. If the operation is successful, we can use the whole allocated vector normally, for example by indexing the p [0] ap [(a-1)].

You’ll also like:

  1. Is it better to use malloc() or calloc()
  2. What is difference between malloc()/free() and new/delete
  3. Memory Allocation Function Calloc()
  4. You have two pairs : new() and delete() and another pair : alloc() and free(). Explain differences between eg. new() and malloc()
  5. Write A C++ Program To Use Malloc To Allocate Memory.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Data Structure

Data Structure Tutorials

  • DS - Home
  • DS - Sorting
  • DS - Shortest Path Algorithm
  • DS - free() Function
  • DS - Graph Traversals
  • DS - Linear Search
  • DS - Heap Sort
  • DS - Searching
  • DS - Linked Lists
  • DS - Algorithms
  • DS - Bubble Sort
  • DS - Quick Sort
  • DS - Binary Search
  • DS - realloc() Vs free()
  • DS - Steps to Plan Algorithm
  • DS - Record Structure
  • DS - Single Linked List
  • DS - Purpose of realloc()
  • DS - Use malloc()
  • DS - calloc() Vs malloc()

Other Links

  • Data Structure - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW