【发布时间】:2015-03-20 16:51:14
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
struct node *pre;
struct node *next;
int data;
}NODE; //struct declaration
int main(){
NODE *new_node=(NODE*)malloc(sizeof(NODE)); //memory allocation
printf("\nnew_node addr: %d\n",new_node);
free(new_node); //deallocation
printf("new_node addr: %d\n",new_node);
}
结果:
new_node addr: 2097152
new_node addr: 2097152
Program ended with exit code: 0
为什么结果是一样的?
我取消分配 new_node 的内存。但是 new_node 有地址。
为什么?
【问题讨论】:
-
出于同样的原因
int a = 5; printf("a=%d\n", a); free(new_node); printf("a=%d\n", a);将打印a=5两次。 -
显然你认为我并没有真正回答你——但我是。就像在我的愚蠢代码中一样,
a的值没有理由改变,new_node的值没有理由改变。