I have researched in all possible ways I could but it's hard for me to digest the fact that both malloc i.e.malloc(sizeof(10))
and calloc i.e. calloc(2,sizeof(5))
allocates same contiguous memory, ignoring the other facts that calloc initializes to zero and works relatively slower than malloc. so this is what I think.
I think that on a 32-bit system if we call malloc and say malloc(sizeof(10))
then malloc will go to the heap and allocate 12 bytes of memory, because for a 32-bit system the memory packages are arranged in groups of 4 bytes so to allocate 10 bytes 3 blocks are needed with a padding of 2 bytes in the last block.
Similarly, if we call calloc and say calloc(2,sizeof(5))
then it will allocate 2 blocks each of size 8 bytes and in total 16 bytes because due to the same reason that memory is in the packages of 4 bytes and to allocate 5 bytes two blocks of 4 bytes are used and in one block a padding of 3 bytes will be provided.
So this is what I think of malloc and calloc. I may be right or wrong but please tell me either way.