Difference between malloc and calloc?

后端 未结 14 1526
感情败类
感情败类 2020-11-22 03:40

What is the difference between doing:

ptr = (char **) malloc (MAXELEMS * sizeof(char *));

or:

ptr = (char **) calloc (MAXEL         


        
14条回答
  •  死守一世寂寞
    2020-11-22 04:19

    Both malloc and calloc allocate memory, but calloc initialises all the bits to zero whereas malloc doesn't.

    Calloc could be said to be equivalent to malloc + memset with 0 (where memset sets the specified bits of memory to zero).

    So if initialization to zero is not necessary, then using malloc could be faster.

提交回复
热议问题