realloc问题 [英] realloc question

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

问题描述

假设我们有:


ptr = malloc(LARGE_AMOUNT);


并且对malloc的调用是成功的(非NULL返回)。


以后有可能再调用realloc:


tmp = realloc(ptr,SMALLER_AMOUNT);


返回NULL?


IOW,当用于*减小*
$的大小时,realloc是否可能失败? b $ b分配内存块?

解决方案

2003年11月7日星期五15:21:40 +0000,Blah写道:
< blockquote class =post_quotes>假设我们有:

ptr = malloc(LARGE_AMOUNT);

并且对malloc的调用是成功的(非NULL返回)。 />
以后是否有可能再调用realloc:

tmp = realloc(ptr,SMALLER_AMOUNT);

返回NULL?

IOW,当用于*减少*分配的内存块的大小时,realloc是否可能失败?




是的。至少标准允许它这样做。

我无法想象它实际上会在大多数实现中发生,但是

你必须经常检查realloc的返回值可以肯定。


在< 8M ******************** @ news4。 srv.hcvlny.cv.net> "布拉赫" < BL ** @ blah.com>写道:

假设我们有:

ptr = malloc(LARGE_AMOUNT);

并且对malloc的调用是成功的(非-NULL返回)。

是否有可能稍后再调用realloc:

tmp = realloc(ptr,SMALLER_AMOUNT);
返回NULL?

IOW,当用于*减少*分配的内存块的大小时,realloc是否可能失败?




comp.std.c派对行是

标准允许的* any *函数失败并不需要失败的实际原因。特别是

,甚至


tmp = realloc(ptr,LARGE_AMOUNT);


允许失败,虽然这应该是一个无操作,并且

如果函数返回ptr或NULL几乎没有区别。


但是有malloc实现有一个

在你的场景中失败的充分理由:分配给

新大小的块的内存池已满并试图通过获取更多内存来扩大它的范围
来自系统的
失败了。通过返回NULL,realloc让你知道

你的内存块仍然有其原始大小。


Dan

- < br $>
Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da ***** @ ifh.de




" Dan Pop" <沓***** @ cern.ch>在消息中写道

news:bo ********** @ sunnews.cern.ch ...

In< 8M ***** ***************@news4.srv.hcvlny.cv.net> "布拉赫" < BL ** @ blah.com>
写道:

假设我们有:

ptr = malloc(LARGE_AMOUNT);

并且对malloc的调用是成功的(非-NULL返回)。

是否有可能稍后再调用realloc:

tmp = realloc(ptr,SMALLER_AMOUNT);
返回NULL?

IOW,当用于*减少* b $ b anallocated内存块的大小时,realloc是否可能失败?
comp.std.c派对line是*
标准允许的任何*函数失败并不需要失败的实际原因。




肯定没有听起来很鼓舞。


这是否意味着:


int fprintf()

{

static int num = 0;

if(num ++%2)

返回real_fprintf();

否则

返回fail_fprintf();

}


符合要求?只是在其他所有电话都失败了,因为感觉好像是

呢?


特别是,甚至

tmp = realloc(ptr, LARGE_AMOUNT);

被允许失败,虽然这应该是一个无操作,如果函数返回ptr或NULL,几乎没有区别。


为了确保我理解你,如果我调用realloc()请求内存块*的大小* * b $ b不会改变*它会失败?

但是有一些malloc实现,在你的场景中有充分的理由导致
失败:分配给新大小的块的内存池已满并尝试通过从系统中获取更多内存来放大它已经失败了。通过返回NULL,realloc让你知道你的内存块仍然有其原始大小。




不是因为内存块减少失败是很难的工作

左右,但出于好奇,是否有任何已知的实现在这种情况下偶尔会失败?


suppose we have:

ptr = malloc(LARGE_AMOUNT);

and the call to malloc is succesful (non-NULL return).

is there any possibility of a later call to realloc like this:

tmp = realloc(ptr,SMALLER_AMOUNT);

to return NULL?

IOW, is it possible for realloc to fail when used to *reduce* the size of an
allocated memory block?

解决方案

On Fri, 07 Nov 2003 15:21:40 +0000, Blah wrote:

suppose we have:

ptr = malloc(LARGE_AMOUNT);

and the call to malloc is succesful (non-NULL return).

is there any possibility of a later call to realloc like this:

tmp = realloc(ptr,SMALLER_AMOUNT);

to return NULL?

IOW, is it possible for realloc to fail when used to *reduce* the size of an
allocated memory block?



Yes. At least the standard allows it to do so.
I can''t imagine it would actually happen on most implementations, but
you must always check the return value of realloc to be sure.


In <8M********************@news4.srv.hcvlny.cv.net> "Blah" <bl**@blah.com> writes:

suppose we have:

ptr = malloc(LARGE_AMOUNT);

and the call to malloc is succesful (non-NULL return).

is there any possibility of a later call to realloc like this:

tmp = realloc(ptr,SMALLER_AMOUNT);

to return NULL?

IOW, is it possible for realloc to fail when used to *reduce* the size of an
allocated memory block?



The comp.std.c party line is that *any* function that is allowed by the
standard to fail doesn''t need an actual reason for failing. In
particular, even

tmp = realloc(ptr, LARGE_AMOUNT);

is allowed to fail, although this should be a no operation and there is
practically no difference if the function returns ptr or NULL.

But there are malloc implementations where there is a good reason for
failing in your scenario: the memory pool allocated for blocks of the
new size is full and the attempt to enlarge it by getting more memory
from the system has failed. By returning NULL, realloc lets you know
that your memory block still has its original size.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de



"Dan Pop" <Da*****@cern.ch> wrote in message
news:bo**********@sunnews.cern.ch...

In <8M********************@news4.srv.hcvlny.cv.net> "Blah" <bl**@blah.com> writes:

suppose we have:

ptr = malloc(LARGE_AMOUNT);

and the call to malloc is succesful (non-NULL return).

is there any possibility of a later call to realloc like this:

tmp = realloc(ptr,SMALLER_AMOUNT);

to return NULL?

IOW, is it possible for realloc to fail when used to *reduce* the size of anallocated memory block?
The comp.std.c party line is that *any* function that is allowed by the
standard to fail doesn''t need an actual reason for failing.



That certainly doesn''t sound encouraging.

Does that mean something along the lines of:

int fprintf()
{
static int num = 0;
if ( num++ % 2 )
return real_fprintf();
else
return fail_fprintf();
}

would be conforming? Just fail on every other call because it feels like
it?

In particular, even

tmp = realloc(ptr, LARGE_AMOUNT);

is allowed to fail, although this should be a no operation and there is
practically no difference if the function returns ptr or NULL.
Just to make sure I understand you, if I call realloc() requesting that the
size of the memory block *does not change* it can fail?
But there are malloc implementations where there is a good reason for
failing in your scenario: the memory pool allocated for blocks of the
new size is full and the attempt to enlarge it by getting more memory
from the system has failed. By returning NULL, realloc lets you know
that your memory block still has its original size.



Not that having a memory block reduction fail is something difficult to work
around, but out of curiosity are there any known implementations that do
occasionally fail in this scenario?


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

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