我想知道 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 吗?