这个分段错误的原因是什么?
它并不总是发生,但当服务器应用程序运行相当长一段时间时就会发生。
原因是什么以及如何解决?
代码如下:
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
It doesn't happen always,but will happen when the server application has been running for quite a while.
What's the reason and how can I fix it?
Code as follows:
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));
core dump:
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于 Host 和 Result 变量都指向合法的内存块,最可能的原因是
Host->h_addr
为NULL
。如果gethostbyname()
或gethostbyaddr()
返回的地址列表为空,就会出现这种情况。我不知道这是怎么造成的(我的 OS X 系统上的文档暗示如果找不到地址,两个函数都应该返回 NULL)。但是,我会在调试器中检查
Host->h_addr_list[0]
进行确认。编辑
调试信息的更新显示了问题所在:
Host->h_addr
为NULL。 h_addr 实际上是一个像这样的#define:其中一个函数返回一个带有空地址列表的
struct hostent
。Given that the Host and Result variables both point to legitimate blocks of memory, the most likely cause is that
Host->h_addr
isNULL
. This would be the case if the list of addresses returned bygethostbyname()
orgethostbyaddr()
were empty.I don't know how that could be caused (the documentation on my OS X system implies that both functions should return NULL if no addresses can be found). However, I would check
Host->h_addr_list[0]
in the debugger to confirm.Edit
The update of the debug info shows where the problem is:
Host->h_addr
is NULL. h_addr is actually a #define like this:One of the functions is returning a
struct hostent
with an empty address list.也许源内存区域和目标内存区域重叠? IE
Maybe, source and destination memory areas overlap? i.e.
memcpy 失败。
这可能是由于
(1) sin_addr 和 h_addr 大小不同或
(2) sin_addr 是一个指针,并且您没有为其分配内存。
请提供结果的定义和初始化以获取更多信息。
The memcpy is failing.
This could be caused by
(1) sin_addr and h_addr are not the same sizes or
(2) sin_addr is a pointer and you have not malloc'd memory for it.
Please provide the definition and initialisation of Result for more info.