Will Faught

December 2016

Why Does Calloc Exist?

Nathaniel J. Smith on the purpose of calloc: When the operating system hands out memory to a process, it always zeros it out first, because otherwise our process would be able to peek at whatever detritus was left in that memory by the last process to use it, which might include, like, crypto keys, or embarrassing fanfiction. So that’s the first way that calloc cheats: when you call malloc to allocate a large buffer, then probably the memory will come from the operating system and already be zeroed, so there’s no need to call memset.

c calloc programming

Will Faught

1 minute