malloc(0)应该返回什么? [英] what malloc(0) should returns?

查看:79
本文介绍了malloc(0)应该返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



对不起,如果这可能是一个愚蠢的问题,但应该返回什么?

malloc(0)?


void * ptr = malloc(0);


我正在运行gcc 3.3.5并返回一个非空地址。


(在我目前正在实现的编译器中,返回NULL。

是不是错误?)


或者应该总是返回一个地址并且

对应当前的堆指针?

Hi,
Sorry if it might be a stupid question but what should returns
malloc(0) ?

void *ptr = malloc(0);

I am running gcc 3.3.5 and a non-null address is returned.

( in the compiler that I am currently implementing, NULL is returned.
Is it wrong ?)

or does an address should be always returned and
correspond to the current heap pointer ?

推荐答案

qu ******* @ yahoo.com 说:



对不起,如果这可能是一个愚蠢的问题,但应该返回什么?
malloc(0)?


void * ptr = malloc(0);
Hi,
Sorry if it might be a stupid question but what should returns
malloc(0) ?

void *ptr = malloc(0);



允许返回NULL,并且允许返回非NULL指针

你可以''取消引用。这两种方式都受到标准的制裁。


但是要小心 - 至少有一种实现(C / 370)用于(并且可能仍然是
)不符合malloc(0) - 该程序将在这样的

电话中异常终止。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

It''s allowed to return NULL, and it''s allowed to return a non-NULL pointer
you can''t dereference. Both ways are sanctioned by the Standard.

Be careful, though - at least one implementation (C/370) used to (and may
still) be non-conforming for malloc(0) - the program would abend on such a
call.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


在comp.lang.c qu ******* @ yahoo.com 写道:
In comp.lang.c qu*******@yahoo.com wrote:

对不起如果它可能是一个愚蠢的问题,但应该返回什么?
malloc(0)?
Sorry if it might be a stupid question but what should returns
malloc(0) ?



7.20.3(n869):


如果请求的空间大小为零,则行为为

实现定义:返回空指针,或者

行为就像大小是非零值一样,除了

返回指针不得用于访问对象。

7.20.3 (n869):

If the size of the space requested is zero, the behavior is
implementation-defined: either a null pointer is returned, or the
behavior is as if the size were some nonzero value, except that the
returned pointer shall not be used to access an object.


我正在运行gcc 3.3.5并返回一个非空地址。
I am running gcc 3.3.5 and a non-null address is returned.



正如允许的那样。

As it is allowed to do.


(在我目前正在实现的编译器中,NULL返回。

这不对吗?)
( in the compiler that I am currently implementing, NULL is returned.
Is it wrong ?)



No.


-

C. Benson Manica |我*应该*知道我在说什么 - 如果我

cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。

No.

--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.


2006年12月18日07:20:53 -0800, qu ******* @ yahoo.com

comp.lang.c中写道:
On 18 Dec 2006 07:20:53 -0800, qu*******@yahoo.com wrote in
comp.lang.c:



对不起,如果这可能是一个愚蠢的问题,但应该返回什么?
malloc(0)?


void * ptr = malloc(0);


我正在运行gcc 3.3.5并返回一个非空地址。


(在编译器中我目前正在实施,返回NULL。

是不是错误?)


或者地址应该总是返回并且

对应当前的堆指针?
Hi,
Sorry if it might be a stupid question but what should returns
malloc(0) ?

void *ptr = malloc(0);

I am running gcc 3.3.5 and a non-null address is returned.

( in the compiler that I am currently implementing, NULL is returned.
Is it wrong ?)

or does an address should be always returned and
correspond to the current heap pointer ?



当前C标准的第7.20.3节部分说明:


"如果要求的空间大小为零,行为是

实现定义:返回空指针,或者

行为就像大小是非零值一样,除了

返回的指针不得用于访问对象。


所以这两种行为都是正确的,但你的编译器的文档需要

记录它做出的选择。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift .com / c ++ - faq-lite /

alt.comp.lang.learn.c- c ++
http:// www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

Section 7.20.3 of the current C standard states in part:

"If the size of the space requested is zero, the behavior is
implementation defined: either a null pointer is returned, or the
behavior is as if the size were some nonzero value, except that the
returned pointer shall not be used to access an object."

So either behavior is correct, but your compiler''s documentation needs
to document which choice it makes.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于malloc(0)应该返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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