先看一个程序:
#include <stdio.h>
#include <malloc.h>
void f(int * q)
{
*q = 200;
//free(q);//错误过早释放动态内存空间
}
int main()
{
int * p = (int *)malloc(sizeof(int));
*p = 10;
printf("%d\n", p);
f(p);
printf("%d\n", p);
return 0;
}
说明在f函数中如果打开注释行free(q);会导致存放200数值的内存空间被释放掉,这样的化*p肯定拿不到被修改后的200了。