关于malloc的问题(0) [英] question on malloc(0)

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

问题描述

int * ptr = malloc(0);

ptr = realloc(ptr,sizeof(int));


这个有效吗?

int *ptr = malloc(0);
ptr = realloc(ptr, sizeof(int));

is this valid?

推荐答案

文章< 66 ************************** ******** @ k37g2000hsf。 googlegroups.com>,

Sri Harsha Dandibhotla< ha ******** @ gmail.comwrote:
In article <66**********************************@k37g2000hsf. googlegroups.com>,
Sri Harsha Dandibhotla <ha********@gmail.comwrote:

> int * ptr = malloc(0);
ptr = realloc(ptr,sizeof(int));
>int *ptr = malloc(0);
ptr = realloc(ptr, sizeof(int));


>这个有效吗?
>is this valid?



根据哪个标准有效? C89 / C90或C99?

-

"有什么可以说的,看,这是新的吗?它已经过去了,这已经是我们面前的旧时代了。 - 传道书

Valid under which standard? C89/C90 or C99?

--
"Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us." -- Ecclesiastes


Sri Harsha Dandibhotla说:
Sri Harsha Dandibhotla said:

int * ptr = malloc(0);

ptr = realloc(ptr,sizeof(int));


这个有效吗?
int *ptr = malloc(0);
ptr = realloc(ptr, sizeof(int));

is this valid?



是的,它是完全合法的,无论原始malloc是否返回NULL

- 但如果你的代码将是不明智的曾经不得不在MVS上的C / 370下工作。


注意,一般来说,你应该在捕获realloc的

结果时使用备用指针,以防它失败。在这种情况下,它几乎没有问题。


-

Richard Heathfield< http:// www。 cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Yes, it''s perfectly legal, whether or not the original malloc returns NULL
- but it is unwise if your code will ever have to work under C/370 on MVS.

Note that, in general, you ought to use a spare pointer when capturing the
result of realloc, just in case it fails. In this case, though, it hardly
matters.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


7月9日下午4:55,rober ... @ ibd.nrc-cnrc.gc.ca(Walter Roberson)
写道:
On Jul 9, 4:55 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:

文章< 660a816a-fdd6-491f-9e34-bd3ff9ae2 ... @ k37g2000hsf.googlegroups.com>,

Sri Harsha Dandibhotla< harsha .... @ gmail.comwrote:
In article <660a816a-fdd6-491f-9e34-bd3ff9ae2...@k37g2000hsf.googlegroups.com>,
Sri Harsha Dandibhotla <harsha....@gmail.comwrote:

int * ptr = malloc(0);

ptr = realloc(ptr,sizeof(int));

这个有效吗?
int *ptr = malloc(0);
ptr = realloc(ptr, sizeof(int));
is this valid?



根据哪个标准有效? C89 / C90还是C99?


Valid under which standard? C89/C90 or C99?



每个标准中发生了什么''malloc(0)''?措辞不是明确的,0没有被提及为特殊情况。

所有ISO C99都说是它分配''size''字节和返回NULL或

指针。

POSIX表示0允许返回唯一指针或NULL。

What happends in each standard with ''malloc(0)''? The wording is not
clear, ''0'' is not mentioned as a special case.
All ISO C99 says is that it allocates ''size'' bytes and returns NULL or
the pointer.
POSIX says 0 is allowed to return a unique pointer or NULL.


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

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