Semantic difference of "Malloc" and Calloc"

I was in the programming class with C and I had doubts about the difference between Malloc and Calloc, but not in what each one does, but in the meaning of "M" and "C".

I know that Malloc comes from memory allocation, already the Calloc I have no idea.

 1
Author: Jefferson Quesado, 2017-11-01

2 answers

If you do a survey, you will find that there is no right answer for the calloc case. The malloc " m " comes from memory allocation. Calloc's "c" is a split issue.

According to this book Linux system Programming there is no official source that defines the meaning of calloc.

But out of curiosity some people believe that the "c" comes from the English word clear which means clear. This is because calloc ensures that the returned piece of memory comes clean and initialized with zero value.

Hope I helped.

 1
Author: Gabriel Mesquita, 2017-11-01 11:30:29

The name calloc() is certainly an abbreviation of clear-allocation, whereas malloc() comes from memory-allocation.

However, the dynamic allocation functions of the default library stdlib.h have different behaviors:

  1. calloc() initializes allocated memory with "zeros";
  2. malloc() does not initialize the allocated memory.

Follows a possible implementation of calloc(), which is basically the combination of the 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;
}
 1
Author: Lacobus, 2017-11-01 14:48:15