The name calloc()
is certainly an abbreviation of clear-allocation, equator malloc()
comes from memory-allocation.
However, the dynamic allocation functions of the standard library stdlib.h
have different behaviors:
calloc()
initializes the memory allocated with "zeros";
malloc()
does not initialize the allocated memory.
Follow a possible implementation of calloc()
, which is basically the combination of functions malloc()
and memset()
:
void * calloc( size_t nmemb, size_t size )
{
size_t len = size * nmemb;
void * p = malloc( len );
memset( p, 0, len );
return p;
}
thank you very much helped, that was the very question that we were dicutindo
– Felipe Porto
Great, remember to accept the question as correct :)
– Gabriel Mesquita
@Felipeport Gabriel’s answer solved your problem? Is it the best available? If so, recommend you mark the question as accepted. You can change your mind about the answer accepted at any time too, if a better answer arises.
– Jefferson Quesado