简体   繁体   English

重新分配会更改指针地址吗?

[英]Does realloc change pointer address?

Take this code for example: 以下面的代码为例:

Struct* allocSomething(void) {
    int n; 
    Struct *something = malloc(n*sizeof(Struct));
    return something;
}

Struct* reallocSomething(Struct **s) {
    int n;
    Struct *something = realloc(*s, (n*sizeof(int)) - 1 * sizeof(Struct));
    return something;
}

int main() {
    Struct *point = allocSomething();
    //code does something...
    point = reallocSomething();
    free(point);
}

My question is, after calling reallocSomething , point has still the same address returned by allocSomething ? 我的问题是,在调用reallocSomethingpoint仍然具有与allocSomething返回的地址相同的地址? For example, if point have address 0x01 , when this pointer get reallocated by reallocSomething , is that address still 0x01 ? 例如,如果point地址为0x01 ,则通过reallocSomething重新分配该指针时,该地址是否仍为0x01

From the man page for realloc : 从手册页进行realloc

 void *realloc(void *ptr, size_t size); 

.... ....

realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from ptr , or NULL if the request fails. realloc()返回一个指向新分配的内存的指针,该指针适合于任何类型的变量, 并且可以与ptr不同 ,如果请求失败,则可以为 NULL。 If size was equal to 0, either NULL or a pointer suitable to be passed to free() is returned. 如果size等于0,则返回NULL或适合传递给free()的指针。 If realloc() fails the original block is left untouched; 如果realloc()失败,则原始块保持不变。 it is not freed or moved. 它不会被释放或移动。

Since realloc may move the allocated memory to a new location, you need to account for this. 由于realloc可能会将已分配的内存移动到新位置,因此您需要考虑这一点。

Don't count on it staying at the same address. 不要指望它停留在同一地址。 The system is allowed to move the whole affair to a different address for whatever reason. 无论出于何种原因,都允许系统将整个事务转移到其他地址。 While the chunk of memory you're requesting is smaller, and it should logically fit in space vacated by the previous allocation, this is not an atomic operation and any interruption or timesharing can have something else grab that memory space. 尽管您请求的内存块较小,并且在逻辑上应该适合先前分配腾出的空间,但这不是原子操作,任何中断或分时操作都可以使内存占用其他空间。 There's probably other shenanigians with whatever system you're using, but standard-wise? 可能还有其他的恶作剧者使用了您正在使用的任何系统,但是在标准方面呢? Don't expect the address to remain the same. 不要期望地址保持不变。 And don't expect it change either. 而且不要期望它也会改变。

From the point of view of the Standard, unless realloc fails, the act of calling it will destroy not just the storage identified by the old pointer, but also the old pointer itself. 从标准的角度来看,除非重新分配失败,否则调用它的行为不仅会破坏旧指针标识的存储,还会破坏旧指针本身。 A compiler may behave in arbitrary and capricious fashion if code tries to access the old pointer other than by examining the bytes thereof, even if the bit pattern in that pointer happen to precisely match those of the new pointer. 如果代码尝试通过检查旧字节而不是访问旧指针,则编译器可能会表现出任意和反复无常的方式,即使该指针中的位模式恰好与新指针的位模式完全匹配。

Quality implementations may (and probably should) allow programs to use realloc in some ways beyond those required by the Standard (eg check whether an object has moved, and recompute pointers that identify portions thereof if it has, but don't bother which such computation if it hasn't moved) but the Standard defines no means via which indications can indicate whether or not they support such constructs. 质量实现可能(并且可能应该)允许程序以某种超出标准要求的方式使用重新分配(例如,检查对象是否已移动,并重新计算标识对象部分的指针(如果有的话),但不必理会此类计算(如果尚未移动),但该标准未定义任何指示可用来指示它们是否支持此类构造的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM