标准C在realloc()之后是否保证相同的内存内容? [英] Does standard C guarantee same memory content after realloc()?

查看:71
本文介绍了标准C在realloc()之后是否保证相同的内存内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我99%肯定标准C保证内存移动

realloc()以防新的,返回的内存块(地址)不同于

的原始内存块。任何C专家都可以向我确认一下吗?


谢谢,

Jonas


PS。我使用的是C90,而不是C99 - 如果它有所不同。

解决方案

文章< rJ ********** *********@newsb.telia.net>,Jonas写道:



我99%肯定标准C保证如果新的返回内存块(地址)与原始内存块(地址)不同,请在内部移动
realloc()。任何C专家都可以向我确认一下吗?




我手里还没有C90标准,但SUSv3(这是一个

ISO C超集)说realloc()

对象的内容应保持不变,直至新的和

旧尺寸中的较小者 ;


-

Andreas K?h?ri


>我99%确定标准C保证在

realloc()内部进行内存移动,以防新的返回内存块(地址)与原始内存块(地址)不同
。任何C专家都可以向我确认一下吗?




内存位置只会在当前位置没有

房间时才会改变对于要求大小的连续区块。


-

cody


[免费软件,游戏和幽默]
www.deutronium.de.vu || www.deutronium.tk


Andreas Kahari< ak ******* @ freeshell.org>写道:

在文章< rJ ******************* @ newsb.telia.net>中,Jonas写道:< blockquote class =post_quotes>
我99%肯定标准C保证在内部移动内部realloc()以防新的返回内存块(地址)与原始地址不同。任何C专家都可以向我确认一下吗?



我手上还没有C90标准,但是SUSv3(这是一个ISO C超集)关于realloc()说
对象的内容应保持不变,直到较小的新旧尺寸。




ISO / IEC 9899:1999声明:


7.20.3.4 realloc功能

[...]

2 realloc函数释放ptr

指向的旧对象,并返回指向大小指定大小为

的新对象的指针。新对象的内容应与解除分配前旧对象的内容相同,直至

新旧尺寸中的较小者。新对象中超出大小

的任何字节旧对象都有不确定的值。

我发现ANSI C89的草稿副本:

4.10.3.4 realloc函数

[...]

realloc函数改变
$ b指向的对象的大小$ b ptr到size指定的大小。对象的内容应保持不变,直至新旧尺寸中的较小者。如果新的

大小较大,则新分配的对象

的值是不确定的。


问候< br $> b $ b -

Irrwahn

(ir*******@freenet.de)


Hi,

I''m 99 % sure that Standard C guarantees to do a memory move inside
realloc() in case the new, returned memory block (address) is different than
the original one. Can any C expert confirm this to me, please?

Thanks,
Jonas

PS. I using C90, not C99--if it makes a difference.

解决方案

In article <rJ*******************@newsb.telia.net>, Jonas wrote:

Hi,

I''m 99 % sure that Standard C guarantees to do a memory move inside
realloc() in case the new, returned memory block (address) is different than
the original one. Can any C expert confirm this to me, please?



I haven''t got the C90 standard in my hand, but SUSv3 (which is a
ISO C superset) says about realloc() that "The contents of the
object shall remain unchanged up to the lesser of the new and
old sizes".

--
Andreas K?h?ri


> I''m 99 % sure that Standard C guarantees to do a memory move inside

realloc() in case the new, returned memory block (address) is different than the original one. Can any C expert confirm this to me, please?



The memory location will only change if at the current position there is no
room for a continous block with the requested size.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk


Andreas Kahari <ak*******@freeshell.org> wrote:

In article <rJ*******************@newsb.telia.net>, Jonas wrote:

Hi,

I''m 99 % sure that Standard C guarantees to do a memory move inside
realloc() in case the new, returned memory block (address) is different than
the original one. Can any C expert confirm this to me, please?



I haven''t got the C90 standard in my hand, but SUSv3 (which is a
ISO C superset) says about realloc() that "The contents of the
object shall remain unchanged up to the lesser of the new and
old sizes".



ISO/IEC 9899:1999 states:

7.20.3.4 The realloc function
[...]
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.
A draft copy of ANSI C89 I found reads:

4.10.3.4 The realloc function
[...]
The realloc function changes the size of the object pointed to by
ptr to the size specified by size . The contents of the object shall
be unchanged up to the lesser of the new and old sizes. If the new
size is larger, the value of the newly allocated portion of the object
is indeterminate.

Regards
--
Irrwahn
(ir*******@freenet.de)


这篇关于标准C在realloc()之后是否保证相同的内存内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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