问题描述
我遇到,&安培;想知道,
I encountered this small piece of code in this question, & wanted to know,
可以在的realloc()
功能不断移动内存块到另一个位置,当内存空间指向的收缩?
Can the realloc()
function ever move a memory block to another location, when the memory space pointed to is shrinked?
int * a = malloc( 10*sizeof(int) );
int * b = realloc( a, 5*sizeof(int) );
如果可能的话,在什么条件下,我能想到 B
来在来自不同地址的
?
If possible, under what conditions, can I expect b
to have an address different from that in a
?
推荐答案
这是可能的的realloc
来的任何呼叫转移内存。真正在许多实现收缩只会导致堆保留大小的变化和不动的记忆。然而,在这对低碎片优化的堆它可以选择四处移动存储到一个更好的安装位置。
It's possible for realloc
to move memory on any call. True in many implementations a shrink would just result in the change of the reserved size in the heap and wouldn't move memory. However in a heap which optimized for low fragmentation it may choose to move the memory around to a better fitting location.
不要依赖的realloc
内存保持在同一个地方进行任何操作。
Don't depend on realloc
keeping memory in the same place for any operation.
这篇关于与收缩的realloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!