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