何时检查malloc的返回值

编程入门 行业动态 更新时间:2024-10-22 21:24:01
本文介绍了何时检查malloc的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我最近在malloc上反映过。 显然,对于20字节的微小分配来strcpy一个文件名或 什么,没有必要检查 malloc的返回值。 OTOH,如果你''为一个大阵列分配一个千兆字节,这可能 失败,所以你一定要检查一个NULL返回。 所以介于这两者之间极端,必须有一个点,你要停止忽略malloc的返回值,然后开始检查它。 人们在哪里画这条线?我想这取决于程序部署的可能系统 ,但是有没有好的经验法则? Rgds, MJ

Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there''s no point putting in a check on the return value of malloc. OTOH, if you''re allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return. So somewhere in between these extremes, there must be a point where you stop ignoring malloc''s return value, and start checking it. Where do people draw this line? I guess it depends on the likely system the program will be deployed on, but are there any good rule-of-thumbs? Rgds, MJ

推荐答案

Marty James写道: Marty James wrote: 你好, 我最近在malloc上反映过。 显然,对于像20字节一样的微小分配来strcpy一个文件名或者 的东西,没有必要检查 malloc的返回值。 Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there''s no point putting in a check on the return value of malloc.

不,总是检查。

No, always check.

OTOH,如果你为一个大的分配一个千兆字节数组,这可能是失败,所以你一定要检查NULL返回。 OTOH, if you''re allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.

不,总是检查。

No, always check.

所以介于这两个极端之间,必须有一个点 你停止忽略malloc的返回值,然后开始检查它。 So somewhere in between these extremes, there must be a point where you stop ignoring malloc''s return value, and start checking it.

不,总是检查。

No, always check.

人们在哪里画这条线? Where do people draw this line?

经常检查。

Always check.

我想这取决于可能的 系统将部署该程序,但有没有任何好的 规则? I guess it depends on the likely system the program will be deployed on, but are there any good rule-of-thumbs?

当然,请务必检查。 Brian

Sure, always check. Brian

我*总是*检查malloc。 为什么画一条线?优化? 甚至谈论画一条线这太特定了。 你画其他线吗?喜欢在哪一点内存泄漏 真的很糟糕?或何时分段错误确实令用户感到烦恼 ? 对不起讽刺但我认为他是计算机的主要角色 程序员将掌控。 I *always* check malloc. Why drawing a line? Optimisation? It''s damn too specific a reason to even talk about "drawing a line". Do you draw other lines? like "at which point does a memory leak really is bad"? or "when is a segmentation fault really that annoying for the user?" Sorry for the sarcasm but I think he primary role of the computer programmer is to be in control.

2008年1月18日星期五15:21:46 -0600,Marty James写道 (文章< sl ******************* @ nospam.invalid>): On Fri, 18 Jan 2008 15:21:46 -0600, Marty James wrote (in article <sl*******************@nospam.invalid>): 你好, 我最近在malloc上反映过。 显然,对于20个字节的微小分配来strcpy一个文件名或 什么,没有必要检查 malloc的返回值。 Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there''s no point putting in a check on the return value of malloc.

为什么?

Why?

OTOH,如果你为大型数组分配一个千兆字节,这个可能 失败,所以你一定要检查NULL返回。 OTOH, if you''re allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.

为什么一个会失败,而另一个失败?我认为你根据你对某一特定尺寸的malloc统计可能性的看法提出索赔

Why can one fail, but not the other? I think you are making claims based upon your perception of the statistical likelihood of a malloc failure for a given size.

所以介于这两个极端之间的某个地方,必须有一个点,你要停止忽略malloc的返回值,然后开始检查它。 So somewhere in between these extremes, there must be a point where you stop ignoring malloc''s return value, and start checking it.

或者你只是一直检查它,而不是假装你知道更多关于malloc内部实现的信息比你实际做的更多。

Or you just check it all the time and not pretend that you know more about the malloc internal implementation than you actually do.

人们在哪里画这条线?我想这取决于程序部署的可能系统 ,但是有没有好的经验法则? Where do people draw this line? I guess it depends on the likely system the program will be deployed on, but are there any good rule-of-thumbs?

我会对任何其他地方绘制线的代码非常谨慎 而不是在所有情况下我们检查malloc返回值。 - 兰迪霍华德(2个删除FOOBAR) 准确观察的力量被那些人称为玩世不恭没有得到它的。 - George Bernard Shaw

I''d be very leery of any code where the line was drawn anywhere other than "in all cases we check malloc return values". -- Randy Howard (2reply remove FOOBAR) "The power of accurate observation is called cynicism by those who have not got it." - George Bernard Shaw

更多推荐

何时检查malloc的返回值

本文发布于:2023-10-28 18:13:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1537389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:返回值   malloc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!