它不會總是發生,但會在服務器應用程序運行很長一段時間時發生。這種分段錯誤的原因是什麼?
什麼原因,我該如何解決?
代碼如下:
struct hostent* Host;
Result->sin_family=AF_INET;
Result->sin_port=htons((unsigned short)Port);
Host=gethostbyname(HostName);
if(!Host)
{
unsigned long int addr=inet_addr(HostName);
if(addr!=-1)
Host=gethostbyaddr(&addr,sizeof(addr),AF_INET);
if(!Host)
{
if(errno!=ETIMEDOUT)
errno=-1; /* use h_errno */
printf("Unknown host for server [%s].", HostName);
return(0);
}
}
memcpy((char*)&Result->sin_addr,(char*)Host->h_addr,sizeof(Result->sin_addr));
核心轉儲:
#0 0x0000000000401913 in proxy_getaddr (HostName=0x7ae30be0 "stackoverflow.com", Port=80, Result=0x7ae30bd0) at proxy.c:529
529 memcpy((char*)&Result->sin_addr,(char*)Host->h_addr,sizeof(Result->sin_addr));
(gdb) p *Host
$4 = {h_name = 0xc4ee048 "", h_aliases = 0xc4ee030, h_addrtype = 2, h_length = 4, h_addr_list = 0xc4ee038}
(gdb) print Result
$5 = (struct sockaddr_in *) 0x7ae30bd0
(gdb) print *Result
$6 = {sin_family = 2, sin_port = 20480, sin_addr = {s_addr = 0}, sin_zero = "\000\000\000\000\000\000\000"}
(gdb) p Host->h_addr_list[0]
$1 = 0x0
(gdb) p Host->h_addr_list
$2 = (char **) 0x1bd9d050
Result定義在哪裏?它可能是未初始化的。 – eyalm 2011-05-23 09:04:53
當您打印結果和*結果時會發生什麼? – nos 2011-05-23 09:05:10
@ eyalm,@ nos,更新了更多信息。 – DriverBoy 2011-05-23 09:08:06