如何在malloc()和calloc()之间进行选择 [英] How to choose between malloc() and calloc()

查看:77
本文介绍了如何在malloc()和calloc()之间进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

虽然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

推荐答案


虽然malloc()和calloc()的目的是相同的,我们也是如此知道calloc()将同步位置初始化为零,
calloc将分配的内存单元的所有位初始化为零,

但是所有

位零不是意味着它等于零。而且malloc()用于字节分配,而calloc()用于内存分配块。
除了这些之外,有任何有力的原因,malloc()是首选的是calloc(),反之亦然?
Though the purpose of both malloc() and calloc() is the same, and as we
also know that calloc() initializes the alloacted locations to ''zero'', calloc initializes all bits of the allocated memory units to ''zero'',
however all
bits zero doesn''t mean it''s equal 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?



因此使用带浮点的calloc()或
$是无用的开销b $ b指针类型,因为内存必须被视为未初始化为

避免未定义的行为。 (参考
http://alien.dowling .edu / ~rohit / wiki ... / C_Programming

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.



lo ********** @ gmail.com 写道:
大家好,
虽然两个malloc()的目的和calloc()是一样的,并且我们也知道calloc()将已经发布的位置初始化为零,
以及malloc()用于字节分配而calloc( )
用于内存分配的大块。
除了这些之外,有没有强烈的理由认为malloc()优先于calloc(),反之亦然?
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?




1)阅读常见问题解答:
http://www.eskimo.com/~scs/C-faq/q7.31.html


2)搜索群组



1) Read the FAQ:
http://www.eskimo.com/~scs/C-faq/q7.31.html

2) Search the group


On Fri,2005年8月26日02:00:18 -0700,lohith.matad写道:
On Fri, 26 Aug 2005 02:00:18 -0700, lohith.matad wrote:
你好所有,
虽然malloc()和calloc()的目的是相同的,并且我们也知道calloc()将已经发布的位置初始化为零,


它将字节初始化为零,这并不能保证浮动

点对象为零或指针为空。

并且malloc()用于字节分配,而calloc()用于内存分配块。


这里真的没有区别,大块的记忆只是一个

字节序列。你在C中分配的内存是可以用来保存一个对象的内存,无论该对象是一个char数组还是一个像结构一样复杂的东西。 malloc()和calloc()可以用于

这两个。

除了这些之外还有任何强烈的理由让malloc()更喜欢
calloc()或反之亦然?
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'',
It initialised the bytes to zero, this doesn''t guarantee that floating
point objects will be zero or pointers will be null.
and also that malloc() is used for bytes allocation whereas calloc()
for chunk of memory allocation.
There''s really no distinction here, a "chunk of memory" is simply a
sequence of bytes. What you allocate in C is memory that can be used to
hold an object, whether that object hppens to be an array of char or
something complex like a structure. malloc() and calloc() can be used for
both of these.
Apart from these is there any strong reason that malloc() is prefered
over calloc() or vice-versa?




如果你想要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()之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆