realloc()隐式free()? [英] realloc() implicit free() ?

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

问题描述

如果realloc()发现有必要移动内存块,那么

是否释放()之前分配的块?


C89标准如果一个

realloc()的内存被realloc()释放,则有一些未定义行为的引用,但唯一的方法是在C89标准中明确提到的
通过realloc释放内存()

将realloc()降低到0字节。


我一直认为它会自动释放以前的内存,

但行为是未定义的[或定义为未发生]?

-

这是一个黄金时代,一个时代的高涨的冒险,丰富的生活和

艰难的死亡......但没有人这么认为。 - Alfred Bester,TSMD

If realloc() finds it necessary to move the memory block, then
does it free() the previously allocated block?

The C89 standard has some reference to undefined behaviour if one
realloc()''s memory that was freed by realloc(), but the only way
explicitly mentioned in the C89 standard to free memory via realloc()
is to realloc() it down to 0 bytes.

I had always assumed it would automatically free the previous memory,
but is the behaviour instead undefined [or defined as not happening] ?
--
"This was a Golden Age, a time of high adventure, rich living and
hard dying... but nobody thought so." -- Alfred Bester, TSMD

推荐答案

Walter Roberson写道:
Walter Roberson wrote:
如果realloc()发现有必要移动内存块,那么它是否释放()先前分配的块?


是。

如果一个
realloc()的内存被realloc释放,C89标准对未定义的行为有一些参考( ),但是在C89标准中明确提到通过realloc()释放内存的唯一方法是将它重新分配()到0字节。


通过_any_

函数传递一个指向realloc指针,指向内存空闲的指针会导致未定义的行为。如果你成功地重新分配了内存导致原始对象被释放的内存,你必须提供一个指向后续由realloc创建的新对象的指针

realloc调用,因为旧对象已被释放。

我一直认为它会自动释放以前的内存,
但行为反而未定义[或定义为不发生]



不,标准很好地定义了这个行为,请参阅

C99的第7.20.3节,我没有C89复制方便。

-
这是一个黄金时代,一个高冒险,丰富生活和艰难死亡的时代......但没有人这么认为。 ; - Alfred Bester,TSMD
If realloc() finds it necessary to move the memory block, then
does it free() the previously allocated block?
Yes.
The C89 standard has some reference to undefined behaviour if one
realloc()''s memory that was freed by realloc(), but the only way
explicitly mentioned in the C89 standard to free memory via realloc()
is to realloc() it down to 0 bytes.
Passing a pointer to realloc that points to memory free''d by _any_
function results in undefined behavior. If you successfully realloc
memory which results in the original object being deallocated, you must
provide a pointer to the new object created by realloc in subsequent
realloc calls as the old object has been freed.
I had always assumed it would automatically free the previous memory,
but is the behaviour instead undefined [or defined as not happening] ?

No, the behavior is well defined by the Standard, see section 7.20.3 of
C99, I don''t have a C89 copy handy.
--
"This was a Golden Age, a time of high adventure, rich living and
hard dying... but nobody thought so." -- Alfred Bester, TSMD



Rob Gamble



Rob Gamble


文章< d6 ** ********@canopus.cc.umanitoba.ca>,

Walter Roberson< ro ****** @ ibd.nrc-cnrc.gc.ca>写道:
In article <d6**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
如果realloc()发现有必要移动内存块,那么
是否释放()先前分配的块?


Quoth n869(7.20.3.4):


[#2] realloc函数解除分配指向的旧对象|

^^^^^^^^^^^^^^^^^^^^^^^^^^

to ptr并返回指向新对象的指针尺寸指定的|

尺寸。新对象|

的内容应与|

释放之前的旧对象的内容相同,直至新旧尺寸中的较小者。 |

新对象中超出旧|

对象大小的任何字节都有不确定的值。 |
[#4] realloc函数返回一个指向new |

对象的指针(可能与指向| <的指针具有相同的值) br />
^^^^^^^^^^^^^^^^^^^^^^^^

旧对象),如果是新的空指针对象不能|

被分配。


所以旧的总是(概念上)被释放,虽然在实践中它是'

非常不可能实际上你会得到一个非动态的realloc调用

释放任何东西(这是as-if规则的一个很好的例子)。

如果一个
realloc()的内存被realloc()释放,C89标准对未定义的行为有一些参考,但是在C89标准中明确提到的唯一方法是通过realloc释放内存()
是将它重新分配()到0字节。


我在这里的C89草案描述了相同的行为,但它更少

清楚如果内存块fgets移动了旧内存被解除分配。


我一直以为它会自动释放以前的内存,
但行为是未定义的[或定义为不发生]?
If realloc() finds it necessary to move the memory block, then
does it free() the previously allocated block?
Quoth n869 (7.20.3.4):

[#2] The realloc function deallocates the old object pointed |
^^^^^^^^^^^^^^^^^^^^^^^^^^
to by ptr and returns a pointer to a new object that has the |
size specified by size. The contents of the new object |
shall be the same as that of the old object prior to |
deallocation, up to the lesser of the new and old sizes. |
Any bytes in the new object beyond the size of the old |
object have indeterminate values. |

[#4] The realloc function returns a pointer to the new |
object (which may have the same value as a pointer to the |
^^^^^^^^^^^^^^^^^^^^^^^^
old object), or a null pointer if the new object could not |
be allocated.

So the old one always gets (conceptually) freed, though in practice it''s
Highly Unlikely that you''ll get a non-moving call to realloc actually
freeing anything (this is an excellent example of the as-if rule).

The C89 standard has some reference to undefined behaviour if one
realloc()''s memory that was freed by realloc(), but the only way
explicitly mentioned in the C89 standard to free memory via realloc()
is to realloc() it down to 0 bytes.
The C89 draft I have here describes the same behavior, but it''s less
clear that if the memory block fgets moved the old memory is deallocated.

I had always assumed it would automatically free the previous memory,
but is the behaviour instead undefined [or defined as not happening] ?



a调用realloc可以执行以下三种操作之一:

-fail调整内存块大小并返回NULL而不取消分配

旧版本

-resize块就地并返回你给它的相同指针

-resize-并移动块的行为相当于做malloc-

memcpy-free自己(除了允许旧的和新的块

重叠 - 我不知道是否有任何合理的方式来实施

它实际上会重叠它们)并返回一个指向新的

内存块的指针


这些都不涉及离开旧的记忆块unfree()d。

dave


-

Dave Vandervies dj ****** @ csclub.uwaterloo.ca


我相信你拼错了痛苦笨"在这句话的最后。

- comp.lang.c中的Peter Seebach



a call to realloc can do one of three things:
-fail to resize the memory block and return NULL without deallocating
the old one
-resize the block in-place and return the same pointer you gave it
-resize-and-move the block with behavior equivalent to doing malloc-
memcpy-free yourself (except that the old and new blocks are allowed
to overlap - I don''t know if there''s any reasonable way to implement
it that actually would overlap them) and return a pointer to the new
memory block

None of these involve leaving old memory blocks unfree()d.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca

I believe you have misspelled "painfully stupid" at the end of this sentence.
--Peter Seebach in comp.lang.c





Walter Roberson写道:


Walter Roberson wrote:
如果realloc()发现有必要移动内存块,那么
是否释放()先前分配的块?


是的。 (好吧,它实际上可能不会调用free(),但

结果将是相同的。)C99 7.20.3.4/2:


realloc函数解除分配旧对象[...]

如果一个
realloc()的内存被realloc()释放,C89标准对未定义的行为有一些参考,但是唯一的方法是在C89标准中明确提到通过realloc释放内存()
是将realloc()降为0字节。


C89的语言并不明确,但在整个部分中

4.3.2它一直提到realloc()作为记忆的方式之一

可以解除分配,在4.10.3.4中它将realloc()描述为

返回指向可能已移动的指针。记忆。我认为

是移动的唯一合理读数。涉及解除分配
以前占用空间的


我一直认为它会自动释放以前的内存,
但行为是未定义的[或定义的]没有发生]?
If realloc() finds it necessary to move the memory block, then
does it free() the previously allocated block?
Yes. (Well, it might not actually call free(), but
the outcome will be the same.) C99 7.20.3.4/2:

The realloc function deallocates the old object [...]
The C89 standard has some reference to undefined behaviour if one
realloc()''s memory that was freed by realloc(), but the only way
explicitly mentioned in the C89 standard to free memory via realloc()
is to realloc() it down to 0 bytes.
C89''s language isn''t as explicit, but throughout section
4.3.2 it keeps mentioning realloc() as one of the ways memory
can be deallocated, and in 4.10.3.4 it describes realloc() as
returning a pointer to the "possibly moved" memory. I think
the only reasonable reading of "moved" involves deallocation
of the previously-occupied space.
I had always assumed it would automatically free the previous memory,
but is the behaviour instead undefined [or defined as not happening] ?




正确的realloc()调用不能调用未定义的行为。

即使C89可以解释为不需要释放旧的内存

,行为就不会被定义 - 你不能用b $ b来引用那个旧内存或任何指向它没有调用

未定义的行为,但只是泄漏它不是UB在最坏的情况下,这将是一个QoI问题,任何实施都会泄露

内存,表现出负面的QoI ......


-
Er*********@sun.com



A proper realloc() call can''t invoke undefined behavior.
Even if C89 could be construed as not requiring the old memory
to be released, the behavior wouldn''t be undefined -- you can''t
refer to that old memory or to any pointer to it without invoking
undefined behavior, but simply leaking it isn''t U.B. At worst
it''d be a QoI issue, with any implementation that leaked the
memory exhibiting negative QoI ...

--
Er*********@sun.com


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

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