施放或不施放malloc? [英] to cast or not to cast malloc ?

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

问题描述

答案都不是。使用宏。


#define ALLOC(大小,类型)((类型)*)malloc((大小)* sizeof(类型))

#define NEW(类型,名称,大小)(类型)*(名称)= ALLOC((大小),(类型))


它们都是[更多]类型安全和简洁。


比较:


NEW(int,x,1000);





int * x =(int *)malloc(sizeof(int)* 1000);

我肯定有人必须发现这之前。无论如何HTH。


MSG

The answer is neither. Use macros.

#define ALLOC(size, type) ((type) *) malloc((size) * sizeof(type))
#define NEW(type, name, size) (type) * (name) = ALLOC((size), (type))

They are both [more] type-safe and concise.

Compare:

NEW(int, x, 1000);

to

int * x = (int*) malloc(sizeof(int) * 1000);
I''m sure someone must have "discovered" this before. Anyways HTH.

MSG

推荐答案

MSG写道:
MSG wrote:
答案都不是。使用宏。

#define ALLOC(大小,类型)((类型)*)malloc((大小)* sizeof(类型))
#define NEW(类型,名称,大小) (类型)*(名称)= ALLOC((大小),(类型))

它们都是[更多]类型安全和简洁。

比较:

NEW(int,x,1000);

int * x =(int *)malloc(sizeof(int)* 1000);


如果你必须比较,至少要比较好的代码:


int * x = malloc(sizeof * x * n);

我肯定有人必须发现这之前。无论如何HTH。
The answer is neither. Use macros.

#define ALLOC(size, type) ((type) *) malloc((size) * sizeof(type))
#define NEW(type, name, size) (type) * (name) = ALLOC((size), (type))

They are both [more] type-safe and concise.

Compare:

NEW(int, x, 1000);

to

int * x = (int*) malloc(sizeof(int) * 1000);
If you must compare, at least compare to good code:

int *x = malloc(sizeof *x * n);
I''m sure someone must have "discovered" this before. Anyways HTH.




是的,他们不是新人,我很害怕。


据推测,你有等价物为所有其他函数返回一个void

指针?不仅仅是标准库函数(memchr,memset,bsearch,

calloc,realloc),还有所有第三方库?


如果不是,你最好忙。当/整套/完成后,完全

测试,并由ISO保证正常工作,我会开始使用它们。

交易?


-

Richard Heathfield: bi **** @ eton .powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton



Yes, they''re hardly new, I''m afraid.

Presumably you have equivalents for all the other functions returning a void
pointer? Not just the standard library functions (memchr, memset, bsearch,
calloc, realloc), but all the third-party libraries, too?

If not, you''d better get busy. When the /whole set/ is completed, fully
tested, and guaranteed by ISO to work correctly, I''ll start using them.
Deal?

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


Richard Heathfield写道:
Richard Heathfield wrote:
如果你必须比较,至少要比较好的代码:

int * x = malloc(sizeof * x * n);
If you must compare, at least compare to good code:

int *x = malloc(sizeof *x * n);




PJ Plauger和Bjarne Stroustrup都不同意你的看法。

这不好代码。

这是一个grand&的坏习惯。进入标准

,这样标准就不会破坏所有现有的坏代码。


请参考comp.lang.c中的最新文章新闻组。



Both P. J. Plauger and Bjarne Stroustrup disagree with you.
It isn''t good code.
It''s a bad habit that was "grandfathered" into the standards
so that the standard wouldn''t break all of the existing bad code.

Please reference recent articles in the comp.lang.c newsgroup.


2004-02-06,E。Robert Tisdale< E. ************** @ jpl.nasa.gov>写道:
On 2004-02-06, E. Robert Tisdale <E.**************@jpl.nasa.gov> wrote:

P. J. Plauger和Bjarne Stroustrup都不同意你的意见。这不是好的代码。这是一个糟糕的习惯,是过时的。进入标准
这样标准就不会破坏所有现有的坏代码。

请参考comp.lang.c新闻组中的最新文章。

Both P. J. Plauger and Bjarne Stroustrup disagree with you. It isn''t
good code. It''s a bad habit that was "grandfathered" into the standards
so that the standard wouldn''t break all of the existing bad code.

Please reference recent articles in the comp.lang.c newsgroup.




我相信Steve Summit的意见远远超过我的意见,所以我们其他人

将离开演员,谢谢。


-Clint



I trust Steve Summit''s opinion far more than I do yours, so the rest of us
will leave off the cast, thanks.

-Clint


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

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