记忆问题 [英] Memory question

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

问题描述



大家好,


根据标准,将malloc和free传递给NULL的结果是什么?


问候,

Mohan。


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++ .moderated。第一次海报:做到这一点! ]


[comp.std.c ++被审核。要提交文章,请尝试发布]

[您的新闻阅读器。如果失败,请使用mailto:st ***** @ ncar.ucar.edu]

[---请在发布前查看常见问题解答。 ---]

[常见问题: http://www.jamesd.demon.co.uk/csc/faq.html ]


Hi All,

As per the standard what is the result of passing NULL to both malloc and free?

Regards,
Mohan.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

推荐答案

2004年2月6日星期五20:33:12 +0000(UTC), mo ****** ******@msn.com

(Mohanasundaram)写道:
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@msn.com
(Mohanasundaram) wrote:

大家好,

根据标准,将malloc和free传递给NULL的结果是什么?


哇,你认为你已经交叉发布到足够多的团体来保证

好​​的答案样本可供选择吗?


我会回复clc帖子(谢谢,代理,给我

意识选项)


将0传递给free()是一个无操作。


将它传递给malloc将返回一个指向

内存的零长度块的指针你可能什么都不做(并且不要忘记

免费)。

-leor

问候,
Mohan 。

[见 http://www.gotw .ca / resources / clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

[comp.std.c ++被审核。要提交文章,请尝试使用]
[您的新闻阅读器]发帖。如果失败,请使用mailto:st ***** @ ncar.ucar.edu]
[---请在发布前查看常见问题解答。 ---]
[常见问题: http:/ /www.jamesd.demon.co.uk/csc/faq.html ]




Leor Zolman

BD软件
le**@bdsoft.com
www.bdsoft.com - 使用C / C ++,Java,Perl& Sons进行现场培训。 Unix

C ++用户:下载BD Software的免费STL错误消息

Decryptor at www.bdsoft.com/tools/stlfilt.html



Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software''s free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html


Leor Zolman写道:
Leor Zolman wrote:
2004年2月6日星期五20:33:12 +0000(UTC), mo ************ @ msn.com
(Mohanasundaram)写道:
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@msn.com
(Mohanasundaram) wrote:

你好全部,

根据标准,将malloc
和free传递给NULL的结果是什么?

Hi All,

As per the standard what is the result of passing NULL to both malloc
and free?



< snip>
将0传递给free()是一个无操作。

将它传递给malloc会返回一个指向零长度内存块的指针,你可以用它来做任何事情(和别忘了取消它。)


<snip>
Passing 0 to free() is a no-op.

Passing it to malloc will return a pointer to a zero-length block of
memory with which you may do absolutely nothing (and don''t forget to
free it).




它可能会合法地返回NULL。


请注意,有些实现不是哈哈ndle malloc(0)好。当你尝试使用malloc(0)时,至少有一个我的熟人实现崩溃。

因此,*即使*标准认可malloc(0),有些明智的

程序员喜欢在实际调用malloc之前检查他们的malloc请求是否为0字节(如果

存在风险)。


-

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



It might, instead, legitimately return NULL.

Note that some implementations don''t handle malloc(0) well. At least one
implementation of my acquaintance crashes when you try to malloc(0).
Consequently, *even though* the Standard endorses malloc(0), some wise
programmers like to check that their malloc request is not for 0 bytes (if
there is a risk of this) before actually calling malloc.

--
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


" Leor Zolman" <乐** @ bdsoft.com>在消息中写道

news:8b ******************************** @ 4ax.com ...
"Leor Zolman" <le**@bdsoft.com> wrote in message
news:8b********************************@4ax.com...
2004年2月6日星期五20:33:12 +0000(UTC), mo ************ @ msn.com
(Mohanasundaram)写道:
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@msn.com
(Mohanasundaram) wrote:

大家好,

根据标准,将NULL传递给

Hi All,

As per the standard what is the result of passing NULL to both


malloc和free是什么结果?
将0传递给free()是无操作。

malloc and free?
Passing 0 to free() is a no-op.

Passing it to malloc will return a pointer to a zero-length block of
memory with which you may do absolutely nothing (and don''t forget to
free it).




OP询问是否将NULL传递给malloc,而不是0. malloc(NULL)可能或者可能

不是编译错误。如果它不是,它的行为就像你描述的那样。


彼得



The OP asked about passing NULL to malloc, not 0. malloc(NULL) may or may
not be a compilation error. In case it isn''t, it behaves as you described.

Peter


这篇关于记忆问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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