主页
C 语言
C 重新分配内存
demo_memory_reallocate
运行 ❯
×
更改方向
保存代码
更改主题
#include
#include
int main() { int *ptr1, *ptr2, size; // Allocate memory for four integers size = 4 * sizeof(*ptr1); ptr1 = malloc(size); printf("%d bytes allocated at address %p \n", size, ptr1); // Resize the memory to hold six integers size = 6 * sizeof(*ptr1); ptr2 = realloc(ptr1, size); printf("%d bytes reallocated at address %p \n", size, ptr2); return 0; }
16 bytes allocated at address 0x5640317512a0
24 bytes reallocated at address 0x5640317512a0