calloc v/s malloc and time efficiency

前端 未结 7 1217
抹茶落季
抹茶落季 2021-02-04 08:12

I\'ve read with interest the post C difference between malloc and calloc. I\'m using malloc in my code and would like to know what difference I\'ll have using calloc instead.

7条回答
  •  野性不改
    2021-02-04 08:52

    For 1 and 2, both do the same thing: allocate and zero, then use the arrays.

    For 3, if you don't need to zero the arrays first, then zeroing is unnecessary and not doing it is faster.

    There is a possibility that calloc's zeroing is more efficient than the code you write, but this difference will be small compared to the rest of the work the program does. The real savings of calloc is not having to write that code yourself.

提交回复
热议问题