calloc()和NULL [英] calloc() and NULL
问题描述
我知道calloc会分配内存并将零写入每个单元格,所以我的问题是:
使用calloc或malloc与运行向其写入NULL的单元格之间有区别吗? calloc的零是否等于NULL?
I know that calloc allocates memory and writes zeroes to each cell, so my question is: is there a difference between using calloc or using malloc and running over the cells writing NULL to them? Are the zeroes of calloc equivalent to NULL?
推荐答案
不,它们并不总是等效,而是在大多数流行的机器上,您都会没事的。 calloc
将全零的位模式写入分配的内存,但是在某些机器上(甚至对于某些类型,空指针值可能并非全零)在某些计算机上)。
No, they are not always equivalent, but on most popular machines you'll be fine. calloc
writes a bit pattern of all-zeros to the allocated memory, but the null pointer value might not be all-bits-zero on some machines (or even just for some types on some machines).
查看空指针部分,提供了大量信息。
Check out the Null Pointers section of the C FAQ for lots and lots of information.
这篇关于calloc()和NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!