#include <stdio.h>
#include<stdlib.h>
typedef struct _Node
{
int data;
}Node;
int main()
{
Node node= (Node)malloc(sizeof(Node));
//赋值
node->data=8;
//打印值
printf(“free before value:%d\n”,node->data);
//打印地址
printf(“free before address:%p\n”,node);
//free掉
free(node);
printf("------------------\n");
//打印值
printf("free later value: %d \n",node->data);
//打印地址
printf("free later address: %p \n",node);
//置空
node=NULL;
//打印地址
printf("null later address: %p \n",node);
return 0;
}
在使用free后我们可以看到 node->data 中的值已经不是8了
但是地址的值仍然没有改变,也就说明指还指向该内存 free只是将里面的值全部抹空
而使用node=NULL后我们可以看到 地址值已经没有了 也就是该指针没有指向任何地址
在释放掉地址后尽量让指针置空 防止指针指向其他地方导致程序崩溃