主页
C 语言
C 重新分配内存
demo_memory_reallocate_null2
运行 ❯
×
更改方向
保存代码
更改主题
#include
#include
int main() { int *ptr1, *ptr2; // Allocate memory ptr1 = malloc(4); // Attempt to resize the memory ptr2 = realloc(ptr1, 8); if (ptr2 == NULL) { printf("Failed. Unable to resize memory"); } else { printf("Success. 8 bytes reallocated at address %p \n", ptr2); ptr1 = ptr2; } // Free memory allocated by malloc or realloc free(ptr1); return 0; }
Success. 8 bytes reallocated at address 0x5640317512a0