Kos*_*mos 0 c++ memory-management realloc dynamic-memory-allocation
该功能可以将存储块移动到新位置,在这种情况下返回新位置.
例如,我有一个指向数组的指针:
int *arr; // somewhere next it initialized, filled with elements and etc
Run Code Online (Sandbox Code Playgroud)
在某个地方我需要:
void* location = realloc(arr, NEW_SIZE);
Run Code Online (Sandbox Code Playgroud)
旧的内存块会发生什么?
如果realloc返回不是数学的指针到arr,我应该使用下一个代码吗?:
delete arr;
arr = (int*)location;
Run Code Online (Sandbox Code Playgroud)