如果新大小更小,realloc可以移动指针吗?

发布于 2024-08-11 19:35:33 字数 400 浏览 1 评论 0原文

我想知道 C 或 C++ 标准是否保证当以较小(非零)大小调用 realloc 时指针不会更改:

size_t n=1000;
T*ptr=(T*)malloc(n*sizeof(T));
//<--do something useful (that won't touch/reallocate ptr of course)
size_t n2=100;//or any value in [1,n-1]
T*ptr2=(T*)realloc(ptr,n2*sizeof(T));
//<-- are we guaranteed that ptr2==ptr ?

基本上,操作系统是否可以自行决定,既然我们释放了一个大内存块,他想要占用利用所有重新分配来对内存进行碎片整理,并以某种方式移动 ptr2 ?

I am wondering whether the C or C++ standard guarantees that a pointer is not changed when realloc is called with a smaller (nonzero) size:

size_t n=1000;
T*ptr=(T*)malloc(n*sizeof(T));
//<--do something useful (that won't touch/reallocate ptr of course)
size_t n2=100;//or any value in [1,n-1]
T*ptr2=(T*)realloc(ptr,n2*sizeof(T));
//<-- are we guaranteed that ptr2==ptr ?

Basically, can the OS decide on its own that since we freed a large memory block, he wants to take advantage of all reallocs to defragment the memory, and somehow move ptr2 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

对岸观火 2024-08-18 19:35:33

http://opengroup.org/onlinepubs/007908775/xsh/realloc.html

成功完成且大小不等于 0 后,realloc() 返回指向(可能已移动的)分配空间的指针。

不,没有保证

http://opengroup.org/onlinepubs/007908775/xsh/realloc.html

Upon successful completion with a size not equal to 0, realloc() returns a pointer to the (possibly moved) allocated space.

Nope, no guarantee

远昼 2024-08-18 19:35:33

无法保证 realloc 会返回相同的位置、周期。

There's no guarantee realloc will return the same location, period.

毁我热情 2024-08-18 19:35:33

使用realloc,您绝对无法保证内存之后的位置。我相信 libc 的默认 malloc 只会勉强复制内存,所以实际上来说你可能没问题。但不要指望它。

With realloc, you get absolutely no guarantees about where the memory will live afterwords. I believe that libc's default malloc will only begrudgingly copy memory around, so practically speaking you may be OK. But don't count on it.

笛声青案梦长安 2024-08-18 19:35:33

realloc 不需要将块保留在适当的位置,即使它适合,事实上,最简单的存根实现是一个可能不需要的示例:

  • malloc:调用 sbrk
  • realloc:调用mallocmemcpy
  • 免费:无操作。

这听起来可能很荒谬,但有时对于嵌入式系统来说,像我刚刚描述的那样的实现实际上是最佳的实现。

realloc is not required to leave the block in place even if it would fit, and in fact the simplest stub implementation is an example where it might not:

  • malloc: call sbrk.
  • realloc: call malloc and memcpy.
  • free: no-op.

This may sound ridiculous, but sometimes for embedded systems an implementation like I've just described is actually the optimal one.

尾戒 2024-08-18 19:35:33

在我看来,所有当前答案(在回答这个问题时)都没有引用任何标准文档。

对于 C++,我将参考 工作草案,编程语言 C++ 标准,文档编号:N3337,日期:2012-01-16,修订:N3291,根据 https://isocpp.org/std/the-standard,是最接近非自由官方C++11标准文档的免费文档;我们在 20.6.13 C 库 中找到:

2 内容与标准C库头文件相同,
进行以下更改:[我认为列出的更改与
问题]。

所以现在我们必须参考C标准。

根据https://stackoverflow.com/a/83763/15485最接近非自由官方C11的免费文档标准文档是编程语言 - C,N1570 委员会草案 — 2011 年 4 月 12 日 ISO/IEC 9899:201x;在这里,我们在 7.22.3.5 处找到了 realloc 函数

4 realloc 函数返回一个指向新对象的指针(
可能与指向旧对象的指针具有相同的值
),或者 null
如果无法分配新对象,则为指针。

我的母语不是英语,因此由您来解释“可能有”的含义。

It seems to me that all the current answers (at the time of this answer) do not refer to any standard document.

For C++ I will refer to Working Draft, Standard for Programming Language C++, Document Number: N3337, Date: 2012-01-16, Revises: N3291 that, according to https://isocpp.org/std/the-standard, is the closest free document to the non-free official C++11 standard document; here we find at 20.6.13 C library:

2 The contents are the same as the Standard C library header ,
with the following changes:[in my opinion the listed changes are not relevant to the
question].

So now we have to refer to the C standard.

According to https://stackoverflow.com/a/83763/15485 the closest free document to the non-free official C11 standard document is Programming languages — C, N1570 Committee Draft — April 12, 2011 ISO/IEC 9899:201x; here we find at 7.22.3.5 The realloc function:

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.

I am not a native English speaker and so is up to you to interpret the meaning of "may have".

放低过去 2024-08-18 19:35:33

在 Windows 上,C 运行时获取堆,然后从该堆分配内存。因此操作系统不会知道各个内存分配,因此不会移动东西。

On Windows, the C-Runtime grabs a heap, and then allocates memory from that heap. So the OS won't know about individual memory allocations, and thus won't move things around.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文