当前位置: 技术问答>linux和unix
free()时出错,大家帮我看看代码哪里出问题了
来源: 互联网 发布时间:2016-08-24
本文导语: #include #include #include #include #include int read_ip_conf() { FILE *fp = NULL; char lines[1024] = {''}; char *first = NULL; if ((fp = fopen("/etc/sysconfig/network-scripts/ifcfg-eth0", "r")) == NULL) ...
#include
#include
#include
#include
#include
int read_ip_conf()
{
FILE *fp = NULL;
char lines[1024] = {''};
char *first = NULL;
if ((fp = fopen("/etc/sysconfig/network-scripts/ifcfg-eth0", "r")) == NULL)
{
return 1;
}
first = (char *)malloc(sizeof(char)*30);
memset(first,0,30);
while (NULL != fgets(lines, 1024, fp))
{
strcpy(first,strtok(lines,"="));
if(strcmp("IPADDR", first)==0)
printf("%sn",strtok(NULL,"n"));
else if(strcmp("NETMASK", first)==0)
printf("%sn",strtok(NULL,"n"));
memset(lines, 0, 1024);
memset(first,0,30);
}
fclose(fp);
free(first);
first = NULL;
return 0;
}
int main()
{
read_ip_conf();
return 0;
}
|
应该是内存越界, 你把数组改大一点就 行了 。
|
内存溢出了