具有calloc的等效malloc [英] Equivalent malloc with calloc

查看:71
本文介绍了具有calloc的等效malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您更喜欢malloc还是calloc?


p = malloc(尺寸);


以下两项中的哪一项是正确的存储与上面的

相同?


p = calloc(1,大小);

p = calloc(size,1) ;

Do you prefer malloc or calloc?

p = malloc(size);

Which of the following two is right to get same storage same as the
above call?

p = calloc(1, size);
p = calloc(size, 1);

推荐答案

4月1日下午4:02,lovecreatesbea ... @ gmail.com

< lovecreatesbea ... @ gmail.comwrote:
On Apr 1, 4:02 pm, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:

你更喜欢malloc还是calloc?
Do you prefer malloc or calloc?



这取决于是否应该将内存初始化为所有位

零。有时它应该,有时它不应该。

That depends on whether the memory should be initialised to all bits
zero. Sometimes it should, sometimes it shouldn''t.


p = malloc(size);


以下哪两个获得与

以上相同的存储空间是正确的吗?


p = calloc(1,size);
p = malloc(size);

Which of the following two is right to get same storage same as the
above call?

p = calloc(1, size);



这里,你要求足够的空间来存储一个元素的数组

的大小[size]。

Here, you''re requesting enough space to store an array with 1 element
of size [size].


p = calloc(size,1);
p = calloc(size, 1);



这里,你要求足够的空间来存储一个数量为[size]

元素大小为1的数组。


最后,两个呼叫请求的大小完全相同。差异

的含义仅对阅读代码的人有意义,而不是对

编译器或计算机有用。

Here, you''re requesting enough space to store an array with [size]
elements of size 1.

In the end, both calls request exactly the same size. The difference
in meaning matters only to anyone reading the code, not to the
compiler or to the computer.

< br>

lovecreatesbea ... @ gmail.com写道:
lovecreatesbea...@gmail.com wrote:

你更喜欢malloc还是calloc?
Do you prefer malloc or calloc?



通常是malloc,但我偶尔也会使用calloc,这样做有利于这样做。

Usually malloc, but I also use calloc occasionally, when it''s
advantageous to do so.


p = malloc(size);


以下哪两项与

相同的存储空间是正确的以上电话?


p = calloc(1,大小);

p = calloc(size,1);
p = malloc(size);

Which of the following two is right to get same storage same as the
above call?

p = calloc(1, size);
p = calloc(size, 1);



两者都分配与malloc调用相同的_amount_存储空间,但是你可能会以这种方式访问存储,以及编译器将翻译你的源代码的方式

肯定会不同于
。当然,您总是可以按照
字节的顺序访问存储空间,但是,恕我直言,这将首先在

中使用calloc的目的。

Both allocate the same _amount_ of storage as the malloc call, but the
manner in which you''d, presumably, access the storage, and the manner
in which the compiler would translate your source would definitely
differ. Of course, you can always access the storage as a sequence of
bytes, but that would, IMHO, defeat the purpose of using calloc in the
first place.


lovecreatesbea ... @ gmail.com说:
lovecreatesbea...@gmail.com said:

你更喜欢malloc还是calloc?
Do you prefer malloc or calloc?



取决于。

It depends.


p = malloc(size);


以下两项中哪一项与上面的

相同的存储空间是正确的?


p = calloc(1,size);

p = calloc(size,1);
p = malloc(size);

Which of the following two is right to get same storage same as the
above call?

p = calloc(1, size);
p = calloc(size, 1);



要么。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Either.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


这篇关于具有calloc的等效malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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