free后地址不会改变 除非赋值为NULL这样活着赋值其他的堆空间地址
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int * p;
p = (int *)malloc(sizeof(int));
if (NULL == p)
{
printf("err\n");
return -1;
}
*p = 111;
printf("*p = %d\n", *p);
printf("before p = %p\n", p);
free(p);
printf("after p = %p\n", p);
p = NULL;
printf("NULL p = %p\n", p);
/*
*p = 111
before p = 00B079B0
after p = 00B079B0
NULL p = 00000000
*/
getchar();
return 0;
}
#include <stdlib.h>
#include <stdio.h>
int main()
{
int * p;
int i;
int n = 10;
//堆区数组,n个int类型的元素,sizeof(int)*n
//首元素为int, 需要int *
//一个malloc出来的空间,它是连续,只需要free一次
p = (int *)malloc(n * sizeof(int) );
int *tmp = p;
for(i = 0; i < n; ++i)
{
*p = i+1;
printf("%d, ", *p);
p++;
}
printf("\n");
free(tmp);
p = NULL;
tmp = NULL;
return 0;
}