如果新大小更小,realloc可以移动指针吗?
我想知道 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
http://opengroup.org/onlinepubs/007908775/xsh/realloc.html
不,没有保证
http://opengroup.org/onlinepubs/007908775/xsh/realloc.html
Nope, no guarantee
无法保证
realloc
会返回相同的位置、周期。There's no guarantee
realloc
will return the same location, period.使用
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.realloc
不需要将块保留在适当的位置,即使它适合,事实上,最简单的存根实现是一个可能不需要的示例:malloc
:调用sbrk
。realloc
:调用malloc
和memcpy
。免费
:无操作。这听起来可能很荒谬,但有时对于嵌入式系统来说,像我刚刚描述的那样的实现实际上是最佳的实现。
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
: callsbrk
.realloc
: callmalloc
andmemcpy
.free
: no-op.This may sound ridiculous, but sometimes for embedded systems an implementation like I've just described is actually the optimal one.
在我看来,所有当前答案(在回答这个问题时)都没有引用任何标准文档。
对于 C++,我将参考 工作草案,编程语言 C++ 标准,文档编号:N3337,日期:2012-01-16,修订:N3291,根据 https://isocpp.org/std/the-standard,是最接近非自由官方C++11标准文档的免费文档;我们在 20.6.13 C 库 中找到:
所以现在我们必须参考C标准。
根据https://stackoverflow.com/a/83763/15485最接近非自由官方C11的免费文档标准文档是编程语言 - C,N1570 委员会草案 — 2011 年 4 月 12 日 ISO/IEC 9899:201x;在这里,我们在 7.22.3.5 处找到了 realloc 函数:
我的母语不是英语,因此由您来解释“可能有”的含义。
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:
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:
I am not a native English speaker and so is up to you to interpret the meaning of "may have".
在 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.