calloc(1,1)vs. * malloc(1)= 0 [英] calloc(1,1) vs. *malloc(1)=0

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

问题描述

要组成一个正确动态分配的空C字符串,是否足够

说:


返回calloc(1,1);


或我最好使用更长的版本


char * p = malloc(1);

if( p)* p = 0;

返回p;


? TIA

To make up a proper dynamically allocated empty C string, would it suffice to
say:

return calloc(1, 1);

or am I better off using the longer version

char *p = malloc(1);
if (p) *p = 0;
return p;

? TIA

推荐答案

rihad< ri *** @ mail.ru>写道:
rihad <ri***@mail.ru> wrote:
要组成一个正确动态分配的空C字符串,是否足以说:

返回calloc(1,1) ;

或者我最好使用更长的版本

char * p = malloc(1);
if(p)* p = 0;
return p;
To make up a proper dynamically allocated empty C string, would it suffice to
say:

return calloc(1, 1);

or am I better off using the longer version

char *p = malloc(1);
if (p) *p = 0;
return p;




两个版本都可能返回NULL,这不是指向空的
字符串的指针。除非你希望函数的调用者检查

NULL,否则你应该在返回p之前采取适当的行动。


至于calloc:我总是很有趣感觉这个空间

初始化为所有位零行为。当你处理未签名的角色权利时,这不是一个问题

,但可能有b $ b令人惊讶的结果。 (引用C理由)使用

其他类型时。因此,我完全不使用它。


HTH

问候

-

Irrwahn

(ir*******@freenet.de)



Both versions may return NULL, which is not a pointer to an empty
string. Unless you expect the caller of your function to check for
NULL you should take appropriate action before returning p.

As for calloc: I always have a funny feeling about this "the space
is initialized to all bits zero" behaviour. That isn''t a problem
when you''re dealing with unsigned character entitities, but may have
"astonishing results" (to quote the C Rationale) when working with
other types. Thus I stay away from using it at all.

HTH
Regards
--
Irrwahn
(ir*******@freenet.de)


" rihad" < RI *** @ mail.ru>在消息中写道

新闻:ai ******************************** @ 4ax.com ...
"rihad" <ri***@mail.ru> wrote in message
news:ai********************************@4ax.com...
要构成一个正确动态分配的空C字符串,是否足以说


返回calloc(1,1);

或者我最好使用更长的版本

char * p = malloc(1);
if(p)* p = 0;
返回p;
To make up a proper dynamically allocated empty C string, would it suffice to say:

return calloc(1, 1);

or am I better off using the longer version

char *p = malloc(1);
if (p) *p = 0;
return p;




这两者之间没有区别,来自C'的POV。


< OT>出于兴趣,你为什么要做这样的事情? :-)< / OT>



There is no difference between those two, from C''s POV.

<OT> Just out of interest, why would you want to do such a thing? :-) </OT>


" rihad" < RI *** @ mail.ru>在消息中写道

新闻:ai ******************************** @ 4ax.com ...
"rihad" <ri***@mail.ru> wrote in message
news:ai********************************@4ax.com...
要构成一个正确动态分配的空C字符串,是否足以说


返回calloc(1,1);

或者我最好使用更长的版本

char * p = malloc(1);
if(p)* p = 0;
返回p;
To make up a proper dynamically allocated empty C string, would it suffice to say:

return calloc(1, 1);

or am I better off using the longer version

char *p = malloc(1);
if (p) *p = 0;
return p;




这两者之间没有区别,来自C'的POV。


< OT>出于兴趣,你为什么要做这样的事情? :-)< / OT>



There is no difference between those two, from C''s POV.

<OT> Just out of interest, why would you want to do such a thing? :-) </OT>


这篇关于calloc(1,1)vs. * malloc(1)= 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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