函数里调用malloc,函数外部释放会报错?
#include <stdio.h>#include <string.h>
#include <stdlib.h>
char *r()
{
char *res = (char*)malloc(sizeof(char) * 8);
res = "1.0.0.0";
return res;
}
int main()
{
char *res = r();
printf("%s\n",res);
free(res);
}
用5.4.0版本gcc编译后到达free那会报错?这是什么原因.
因为res赋值后不再是原来的地址了
话说strcpy这个函数你们初学者都不知道的么 回复 2# yshwuxian
忘了res是指针了,谢拉
页:
[1]