Difference in uses between malloc and calloc

前端 未结 7 461
花落未央
花落未央 2020-12-22 05:00
gcc 4.5.1 c89

I have written this source code for my better understanding of malloc and calloc.

I understand, but just have a few questions

7条回答
  •  失恋的感觉
    2020-12-22 05:33

    As you point out, calloc zeroes out the memory is allocates, while malloc doesn't.

    Your examples 1 & 2 each allocate a single contiguous block of five structs (and returns a pointer to that block), whereas example 3 allocates five separate blocks of one struct each (and gives you five pointers unrelated to one another.)

提交回复
热议问题