何时realloc失败? [英] when can realloc fail?

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

问题描述



我只是想把它做对。

调用realloc()会在出错时返回NULL并且原始内存为

保持不变,

请求更大或更小尺寸的原件,

对吧?

但是调用realloc大小设置为零的()相当于free(),

返回void。

这是否意味着在重新调用内存时对realloc()的调用可能会失败br />
除非

将其缩小为零,在这种情况下它总会成功?


谢谢!

解决方案

ba ****** @ hushmail.com 写道:




我只想这样做。

致电realloc()将在出错时返回NULL并且原始内存

保持不变,

当请求更大或更小的尺寸时,原始的,

R ight?

但是调用大小设置为零的realloc()等同于free(),

返回无效。

这意味着调用realloc()可能会在内存消失时失败

除非

将其缩小为零,在这种情况下它总能成功?



是的,realloc()在缩小分配时可能会失败,或者在增长时可能会失败,或者甚至在保持大小不变​​的情况下也会失败。 br />

当重新分配到零大小时,情况很复杂。

C90说如果size为零且ptr不是空指针,那么

它指向的对象被释放。由于free()不能失败(给定

一个不会调用未定义行为的参数),因此它遵循

realloc(...,0)不会失败。


但是C99没有这样的文字,并没有特别的大小

零。所有我们被告知的是realloc(...,0)要么失败要么

返回指向大小为零的对象的指针。我找不到任何东西
$ C $ b在C99禁止realloc(...,0)失败,所以大概可以。


-

Eric Sosman
es*****@acm-dot-org.inva


ba ****** @ hushmail.com 写道:




我只是想做对。

调用realloc( )将在错误
上返回NULL



在这种情况下,单词失败可能比

字错误更合适。
< blockquote class =post_quotes>
并且原始内存保持不变,

在请求更大或更小的原始版本时,对吗?



是。


但是调用大小设置为零的realloc()等同于免费(),



语义上是。


返回无效。



不,它返回一个不确定的值。


这是否意味着调用realloc()当内存消失时可能会失败,除非

将其缩小为零,在这种情况下它总能成功吗?



即使新尺寸为零,也不能保证不会出现编号。


4月1日晚上9点06分,Eric Sosman< esos ... @ acm-dot-org.invalidwrote:


banan ... @hushmail。 com写道:




我只是想做对。

调用realloc( )将在错误时返回NULL并且原始内存

保持不变,

请求更大或更小尺寸的原始内容,

对吗?

但是调用大小设置为零的realloc()等同于free(),

返回无效。

这是否意味着调用realloc()可能会在内存消失时失败

除非

将其缩小为零,在这种情况下它总会成功?



是的,realloc()在缩小分配时可能会失败,或者在增长时会失败,或者甚至在保持大小不变​​的情况下也会失败。


当重新分配到零大小时,情况很复杂。

C90说如果size为零且ptr不是空指针,那么

它指向的对象被释放。由于free()不能失败(给定

一个不会调用未定义行为的参数),因此它遵循

realloc(...,0)不会失败。


但是C99没有这样的文字,并没有特别的大小

零。所有我们被告知的是realloc(...,0)要么失败要么

返回指向大小为零的对象的指针。我找不到任何东西在C99中禁止
禁止realloc(...,0)失败,所以大概可以。



感谢您的回复!

但我想知道,如果realloc(...,0)在C99中失败,我该怎么办?知道吗?

我怎么知道返回的NULL是否意味着realloc()失败,

或者它是否是指向新内存的指针,为空,这是NULL 。

(这就是我在编译器上得到的)


Hi,
I just want to get this right.
A call to realloc() will return NULL on error and the original memory
is left untouched,
both when requesting a larger or a smaller size that the original,
right?
But a call to realloc() with size set to zero is equivalent to free(),
with returns void.
Does that mean that a call to realloc() can fail when shinking memory
except when
shrinking it to zero in which case it will always succeed?

Thanks!

解决方案

ba******@hushmail.com wrote:

Hi,
I just want to get this right.
A call to realloc() will return NULL on error and the original memory
is left untouched,
both when requesting a larger or a smaller size that the original,
right?
But a call to realloc() with size set to zero is equivalent to free(),
with returns void.
Does that mean that a call to realloc() can fail when shinking memory
except when
shrinking it to zero in which case it will always succeed?

Yes, realloc() can fail when shrinking an allocation, or
when growing it, or even when leaving its size unchanged.

When reallocating to zero size, the situation is complicated.
C90 says "If size is zero and ptr is not a null pointer, the
object it points to is freed." Since free() cannot fail (given
an argument that doesn''t invoke undefined behavior), it follows
that realloc(...,0) cannot fail.

But C99 has no such text, and makes no special case for size
zero. All we''re told is that realloc(...,0) either fails or it
returns a pointer to an object of size zero. Nothing I can find
in C99 forbids realloc(...,0) to fail, so presumably it can.

--
Eric Sosman
es*****@acm-dot-org.invalid


ba******@hushmail.com wrote:

Hi,
I just want to get this right.
A call to realloc() will return NULL on error

The word failure might be more appropriate in this context than the
word error.

and the original memory is left untouched,
both when requesting a larger or a smaller size that the original, right?

Yes.

But a call to realloc() with size set to zero is equivalent to free(),

Semantically yes.

with returns void.

No, it returns an indeterminate value.

Does that mean that a call to realloc() can fail when shinking memory except when
shrinking it to zero in which case it will always succeed?

No. realloc is not guaranteed to suceed even when the new size is zero.


On Apr 1, 9:06 pm, Eric Sosman <esos...@acm-dot-org.invalidwrote:

banan...@hushmail.com wrote:

Hi,
I just want to get this right.
A call to realloc() will return NULL on error and the original memory
is left untouched,
both when requesting a larger or a smaller size that the original,
right?
But a call to realloc() with size set to zero is equivalent to free(),
with returns void.
Does that mean that a call to realloc() can fail when shinking memory
except when
shrinking it to zero in which case it will always succeed?


Yes, realloc() can fail when shrinking an allocation, or
when growing it, or even when leaving its size unchanged.

When reallocating to zero size, the situation is complicated.
C90 says "If size is zero and ptr is not a null pointer, the
object it points to is freed." Since free() cannot fail (given
an argument that doesn''t invoke undefined behavior), it follows
that realloc(...,0) cannot fail.

But C99 has no such text, and makes no special case for size
zero. All we''re told is that realloc(...,0) either fails or it
returns a pointer to an object of size zero. Nothing I can find
in C99 forbids realloc(...,0) to fail, so presumably it can.

Thank you for your reply!
But I wonder, if realloc(...,0) fails in C99, how can I know that?
How can I know if a returned NULL means that realloc() failed,
or if it is the pointer to the new memory, empty, which is NULL.
(That is what I got on my compiler anyway)


这篇关于何时realloc失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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