2

Possible Duplicate:
c difference between malloc and calloc
why malloc+memset slower than calloc?

What's the difference between calloc & malloc followed by a memset? If I replace all calls to calloc with a malloc followed by a memset, will it be the same?

If that is the case, then why are two functions malloc & calloc seperately provided?

Community
  • 1
  • 1
Jay
  • 24,173
  • 25
  • 93
  • 141
  • 4
    http://stackoverflow.com/questions/2688466/why-mallocmemset-slower-than-calloc – cnicutar May 22 '12 at 05:59
  • 1
    there is many, many answers in Internet and here – theWalker May 22 '12 at 06:07
  • Refer http://stackoverflow.com/questions/2605476/calloc-v-s-malloc-and-time-efficiency and http://stackoverflow.com/questions/1538420/c-difference-between-malloc-and-calloc – verisimilitude May 22 '12 at 06:31
  • @All, This is not homework. My question here was a little more specific "If calloc = malloc + memset", and you have malloc and memset already available to you, then why is calloc required at all? I guess http://stackoverflow.com/a/1585987/236222 answers it to some extent. None of the answers have also been able to establish that calloc's performance is better than malloc. So, the question was to know the intention of two functions being provided? Hope this clears the air. – Jay May 22 '12 at 07:01

2 Answers2

13

While calloc() always initialises the memory area with zeroes ('\0'), the memset() call allows you to choose which bytes to fill the memory with.

In terms of speed, calloc() is likely to be faster than malloc() + memset() if memory needs to be zeroed out; malloc() returns uninitialised memory faster but it still requires an additional call to memset().

Basically, if you want to zero out the memory, use calloc(); if you want to leave it uninitialised, use malloc().

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
3

One important difference is that I expect calloc(nmemb, size) to return NULL if nmemb * size overflows. If you instead use malloc(nmemb * size), multiplicative overflow can cause you to request a smaller buffer than you expected (which could lead to security problems later). Therefore if you're planning to replace calloc calls with malloc ones, some care is necessary.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • "With malloc, and you'll end up requesting smaller buffer than you expected" - How is this possible? Won't malloc fail if it cannot provide the requested number of bytes? Would be great if you could clarify it in more detail. – Jay May 22 '12 at 07:14
  • 1
    @Jay: Yes, `malloc` will fail if it can't provide the requested number of bytes. The problem here is that the *request* itself can be wrong if you don't check for multiplicative overflow. – jamesdlin May 22 '12 at 07:47
  • @jamesdlin that's not in the standard, so I hope you do bounds checking yourself ;-) – Ja͢ck May 22 '12 at 07:59
  • @Jack It might not be explicitly stated in the standard, but I think that it's implicit by the requirement that `calloc` return `NULL` if it can't honor the request. I do acknowledge that there may be a quality-of-implementation issue, though. – jamesdlin May 22 '12 at 08:10