主页
C 语言
C 释放内存
demo_memory_free
运行 ❯
×
更改方向
保存代码
更改主题
#include
#include
int main() { int *ptr; ptr = malloc(sizeof(*ptr)); // Allocate memory for one integer // If memory cannot be allocated, print a message and end the main() function if (ptr == NULL) { printf("Unable to allocate memory"); return 1; } // Set the value of the integer *ptr = 20; // Print the integer value printf("Integer value: %d\n", *ptr); // Free allocated memory free(ptr); // Set the pointer to NULL to prevent it from being accidentally used ptr = NULL; return 0; }
Integer value: 20