C语言疑问
常如意 2014-01-09 09:31:15 程序1:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fun(char**point)
{
*point = malloc(5);
ASSERT(*point != NULL);
}
int main()
{
char* p;
fun(&p);
free(p);
return 0;
}
程序2:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char* p, point;
p = malloc(5);
ASSERT(p != NULL);
point = p;
free(point);
return 0;
}
问题:
程序1中从被调函数中分配的内存,在主调函数中释放;
程序2中两个指针指向同一内存,释放point而不是p,上述两种做法会不会有什么不妥?怎样修改?恳请指点。