The semantic difference of "Malloc" and Calloc "

1

I was in the programming class with C and I was wondering 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 Calloc I have no idea.

    
asked by anonymous 01.11.2017 / 12:22

2 answers

1

If you do a search, you will find that there is no right answer to the calloc case. The "m" of malloc comes from memory allocation. Calloc's "c" is a divided question.

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

But as a matter of curiosity some people believe that the "c" comes from the English word clear which means to clean. This is because calloc ensures that the returned piece of memory comes cleaned and initialized to zero.

I hope I have helped.

    
01.11.2017 / 12:30
1

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

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

  • calloc() initializes memory allocated with "zeros";
  • malloc() does not initialize the allocated memory.
  • Here is a possible implementation of calloc() , which basically is the combination of the malloc() and memset() functions:

    void * calloc( size_t nmemb, size_t size )
    {
        size_t len = size * nmemb;
        void * p = malloc( len );
        memset( p, 0, len );
        return p;
    }
    
        
    01.11.2017 / 15:42