Semantic difference of "Malloc" and Calloc"

Asked

Viewed 451 times

1

I was in programming class with C and I got the doubt 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.

2 answers

1

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:

  1. calloc() initializes the memory allocated with "zeros";
  2. 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;
}

1

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

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

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

I hope I’ve helped.

  • thank you very much helped, that was the very question that we were dicutindo

  • Great, remember to accept the question as correct :)

  • @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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.