Monday 27 March 2017

c - What's the difference between calloc & malloc followed by a memset?

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().

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...