Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:07:27+00:00 2026-06-11T14:07:27+00:00

calloc allocates num blocks of memory, each of size size : void * calloc

  • 0

calloc allocates num blocks of memory, each of size size:

void * calloc ( size_t num, size_t size );

Allocate space for array in memory
Allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.

In contrast, malloc allocates one block of memory of size size:

void * malloc ( size_t size );

Allocate memory block
Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.


Is there any difference between both (except for the zero-initialization by calloc)?

What does calloc means exactly by num blocks of memory as in practice the returned memory region is contiguous as well.

I believe there has to be some difference, otherwise it wouldn’t make much sense to define two different interfaces for these methods?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:07:29+00:00Added an answer on June 11, 2026 at 2:07 pm

    In practice they do the same thing. The advantage of calloc is that good implementations will perform an overflow detection when doing the multiplication needed to determine how much memory you need.

    If you do it something like this:

    void *
    calloc(size_t nmemb, size_t size)
    {
        size_t sz = nmemb * size;
        void *res = malloc(sz);
    

    ‘sz’ might not end up being what you expect it to be. malloc will then allocate much less than the caller expected, but the caller can end up treating the returned area as large enough. This leads to heap overflows will all the security implications that usually has.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does calloc allocate return contiguous memory location? If yes, what would it do if
I'm trying to allocate a block of memory and then copy data into that
I am declaring an array of void pointers. Each of which points to a
I want to know that how can we allocate a memory block at run-time
Let's say I've calloc'd some memory for myself: byte *header = calloc(5 + ZHEADERSIZE,
I create a 2D dynamic array: a = (int**)calloc(n-1, sizeof(int)); for(i = 0; i
It's known that calloc is different than malloc in that it initializes the memory
What does this statement mean? //allocated memory for Device info (*PppsCoreStructure)->psDeviceDetails=(sDeviceDetails **)calloc(CORE_DEVICEINFO_SIZE, sizeof(sDeviceDetails*)); I
From what I understand, I have to choose between calloc , which will allocate
I am using a 2D array, and am required to allocate it as shown:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.