1 #include<netdb.h> 2 #include<stdio.h> 3 #include<stdlib.h> 4 #include<string.h> 5 //#include<unp.h> 6 int main(){ 7 struct servent *serv=(struct servent * )calloc(1,sizeof(struct servent)); 8 9 serv=getservbyname("domain","tcp"); 10 if(serv==NULL){ 11 perror("gethostbyname ----:"); 12 exit(EXIT_FAILURE); 13 } 14 //serv[1]=(struct servent)NULL; 15 printf("s_name %s\n",serv->s_name); 16 printf("s_aliases %s\n",*serv->s_aliases); 17 printf("s_port %d\n",serv->s_port); 18 printf("s_proto %s\n ",serv->s_proto); 19 20 printf("-------------\n"); 21 //bzero(serv,sizeof(struct servent)); 22 23 24 struct servent* ser=getservbyport(13568,"tcp"); 25 printf("s_name %s\n",ser->s_name); 26 printf("s_aliases %s\n",*ser->s_aliases); 27 printf("s_port %d\n",ser->s_port); 28 printf("s_proto %s\n ",ser->s_proto); 29 30 printf("-------------\n"); 31 //ser[1]=(struct servent)NULL; 32 //free(&ser[0]); 33 //free(&serv[0]); 34 free(ser); 35 free(serv); 36 return 0; 37 }
出现内存释放错误,查看好多资源,貌视没有很好的解决,请大神指点
*** glibc detected *** ./getservby: free(): invalid pointer: 0x00268f48 ***
据目测
struct servent *serv=(struct servent * )calloc(1,sizeof(struct servent));
serv=getservbyname("domain","tcp");
你的serv有通过calloc分配空间
struct servent* ser=getservbyport(13568,"tcp");
而你的ser却没有,问题可能出在这里
一语惊醒梦中人,谢谢了,