问题描述
大家好,
虽然malloc()和calloc()的目的是相同的,而且我们
也知道calloc()初始化了alloacted位置为零,
以及malloc()用于字节分配,而calloc()
用于内存分配块。
除了这些之外,还有什么强烈的理由要求malloc()优先于calloc()
,反之亦然?
期待你的澄清,可能详细。
提前致谢
Lohi
Hi all,
Though the purpose of both malloc() and calloc() is the same, and as we
also know that calloc() initializes the alloacted locations to ''zero'',
and also that malloc() is used for bytes allocation whereas calloc()
for chunk of memory allocation.
Apart from these is there any strong reason that malloc() is prefered
over calloc() or vice-versa?
Looking forward for your clarrifications , possibly detailed.
Thanks in advance
Lohi
推荐答案
因此使用带浮点的calloc()或
$是无用的开销b $ b指针类型,因为内存必须被视为未初始化为
避免未定义的行为。 (参考
calloc()和malloc()有什么区别?)
期待您的澄清,可能详细。
It is therefore useless overhead to use calloc() with floating point or
pointer types, because the memory must be treated as uninitialized to
avoid undefined behaviour. (reference
http://alien.dowling.edu/~rohit/wiki.../C_Programming
What is the difference between calloc() and malloc()? )
Looking forward for your clarrifications , possibly detailed.
1)阅读常见问题解答:
2)搜索群组
1) Read the FAQ:
http://www.eskimo.com/~scs/C-faq/q7.31.html
2) Search the group
如果你想要all-bytes-zero那么使用calloc()是有意义的,但是再次
记住它不保证浮点或
指针对象的初始值。如果你不需要那个,那么calloc()仍然具有归零内存的
开销。 malloc()更常用,因为它更简单并且将所有字节设置为零通常不是初始化
所需。
劳伦斯
If you want all-bytes-zero then it makes sense to use calloc(), but again
remember that it doesn''t guarantee initial value for floating point or
pointer objects. If you don''t need that then calloc() still has the
overhead of zeroing memory. malloc() is more commonly used because it is
simpler and setting all bytes to zero is typically not the initialisation
needed.
Lawrence
这篇关于如何在malloc()和calloc()之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!