malloc vs calloc [英] malloc vs calloc
问题描述
之间是否有区别:
/ *代码1 * /
结构样本测试;
test = malloc (sizeof(struct sample));
memset(& test,0,sizeof(test));
/ * code 2 * /
struct sample test;
test = calloc(1,sizeof(struct sample));
为什么选择代码1代码2?我倾向于在源代码中看到很多
代码1的实例,几乎没有任何代码实例2.
谢谢
David
" David Hill" <哒*** @ wmol.com>在消息中写道
news:GnEpb.74071
275.194415@attbi_s53 ...是否有区别:
巨魔。你不是刚问这个吗?
Tom
11月4日星期二2003年,David Hill写道:
之间有区别吗:
/ *代码1 * /
结构样本测试;
test = malloc(sizeof(struct sample));
哎呀!纠正了下面明显的错别字:
/ *代码1 * /
struct sample * test;
test = malloc(sizeof * test);
memset(test,0,sizeof * test);
/ * code 2 * /
struct sample * test;
test = calloc(1,sizeof * test);
为什么代码1会被选为代码2?我倾向于在源代码中看到许多代码1的实例,几乎没有代码2的实例。
OMMV,但我个人避免''calloc' ',以及一些图书馆
函数,例如''memmove'',因为我认为他们的*确切*效果
是模糊的,我也没有感觉到就像在阅读联机帮助页面时,我不需要这样做。
特别是对于你的例子,''calloc''有两个参数,其中>
订单很重要 - 所以我必须做到这一点,''malloc''
我不能。易于使用。
其次,''calloc''尝试将所有新分配的字节设置为零。
通常需要O(n)时间。现在C一般在C语言语句和机器级
指令之间有一个非常接近的
对应关系,我觉得如果我将要执行一个非常昂贵的设置步骤,例如将整个块设置为零,那个'b $ b'应该*不要隐藏在库函数中的东西
调用。 ''malloc''/''memset''成语使设置''* test''
的成本略微更加明显。
这些是我不使用''calloc''的两个重要原因,但又有一个b $ b:它很少是适合这项工作的正确工具。
实际上,*这些片段中的*都不会做你想要的,
在一般情况下:将struct'的字节设置为全为零不是
与将该结构的每个成员设置为零相同,尤其是当它指向指针和浮点数时。没有简单的方法将动态分配的''struct''的每个成员设置为零,
虽然 - 你必须一个一个地做-one。
HTH,
-Arthur
Is there a difference between:
/* code 1 */
struct sample test;
test = malloc(sizeof(struct sample));
memset(&test, 0, sizeof(test));
/* code 2 */
struct sample test;
test = calloc(1, sizeof(struct sample));
Why would code 1 be chosen over code 2? I tend to see many instances of
code 1 in source code and hardly any instances of code 2.
Thanks
David
"David Hill" <da***@wmol.com> wrote in message
news:GnEpb.74071
275.194415@attbi_s53...Is there a difference between:
Troll. Didn''t you just ask this?
Tom
On Tue, 4 Nov 2003, David Hill wrote:
Is there a difference between:
/* code 1 */
struct sample test;
test = malloc(sizeof(struct sample));
Whoops! Corrected the obvious typos below:
/* code 1 */
struct sample *test;
test = malloc(sizeof *test);
memset(test, 0, sizeof *test);
/* code 2 */
struct sample *test;
test = calloc(1, sizeof *test);
Why would code 1 be chosen over code 2? I tend to see many instances of
code 1 in source code and hardly any instances of code 2.
OMMV, but I personally avoid ''calloc'', along with some library
functions such as ''memmove'', because I think their *exact* effects
are obscure, and I don''t ever feel like reading manpages when I
don''t have to.
In particular for your example, ''calloc'' takes two arguments whose
order matters -- so I''d have to get that right, where with ''malloc''
I don''t. Ease of use.
Secondly, ''calloc'' tries to set all the newly allocated bytes to zero.
That generally takes O(n) time. Now C in general has a very close
correspondence between C-language statements and machine-level
instructions, and I feel that if I''m going to be performing a very
expensive setup step like "set this whole block to zero," that''s
something that should *not* be hidden away inside a library function
call. The ''malloc''/''memset'' idiom makes the cost of setting ''*test''
to zero slightly more noticeable.
Those are the two big reasons I don''t use ''calloc'', but there''s
one more: it is rarely the right tool for the job.
In fact, *neither* of those fragments is going to do what you want,
in the general case: setting a struct''s bytes to all zero isn''t the
same as setting each member of that struct to zero, especially when
it comes to pointers and floating-point numbers. There''s no easy
way to set each member of a dynamically allocated ''struct'' to zero,
though -- you have to do it one-by-one.
HTH,
-Arthur
这篇关于malloc vs calloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!