void 指针有条件引发段错误
当我们处理空的前一个或下一个指针时,我使用双链表并在边缘情况下获得一些奇怪的性能。 GDB 返回以下错误:
Program received signal SIGSEGV, Segmentation fault.
0x0804a9c1 in DLinkDelete (delete=0xd8c9d33c) at test.c:213
213 if (prevdl && prevdl->next)
(gdb) p prevdl
$39 = (DoubleLink *) 0xdadadada
(gdb) p prevdl->next
$40 = (void *) 0x0
DoubleLink 是具有以下格式的结构:
typedef struct
{
void *next;
void *prev;
} DoubleLink;
为什么会在此处引发分段错误?
I'm using a double-linked list and getting some odd performance regarding edge cases when we're dealing with previous or next pointers that are null. GDB returns the following error:
Program received signal SIGSEGV, Segmentation fault.
0x0804a9c1 in DLinkDelete (delete=0xd8c9d33c) at test.c:213
213 if (prevdl && prevdl->next)
(gdb) p prevdl
$39 = (DoubleLink *) 0xdadadada
(gdb) p prevdl->next
$40 = (void *) 0x0
DoubleLink is a struct with the following format:
typedef struct
{
void *next;
void *prev;
} DoubleLink;
Why would a segmentation fault be raised here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
大概
prevdl
指向无效内存?拜托,0xdadadada
对您来说真的是一个有效的地址吗? “如果是精神分裂症,那就太白痴了”。Presumably
prevdl
points to invalid memory ? Come on, does0xdadadada
really look like a valid address to you ? "It's too idiotic to be schizophrenic".0xdadadada
地址看起来很奇怪,尽管它不为 NULL。也许你正在尝试读取你不允许的记忆。the
0xdadadada
address seems very weird although it is not NULL. Maybe you're trying to read a memory that is not allowed to you.该值:
看起来与真实的指针值可疑。仅仅因为指针不为 NULL 并不意味着它是有效的。如果你使用无效的指针,你会得到aseg 错误,或者其他什么。
The value:
looks suspiciously unlike a real pointer value. Just because a pointer is not NULL does not mean it is valid. And if you use an invalid pointer, you will get aseg fault, or whatever.
0xdadadada
看起来有点可疑,也许prevdl
还没有正确初始化?或者也许它被其他一些代码踩踏了。也许您应该尝试在 valgrind 下运行测试,看看它是否报告任何内容?
0xdadadada
looks a bit suspicious, perhapsprevdl
hasn't been properly initialised? Or maybe it's been stomped on by some other code.Maybe you should try running the test under valgrind and see if it reports anything?
0xdadadada
听起来不太可能是有效指针的值……很可能是调试初始化程序。0xdadadada
doesn't sound like a very likely value for a valid pointer... most likely a debugging initialiser.具体来说,它意味着您正在访问尚未被访问的数据。完全初始化。问题——这是第一次尝试使用这种结构吗?
无论如何,请检查您的初始化代码;当您创建节点时,我敢打赌第一个节点没有前一个节点,您没有设置它的值,但在上面的代码中您试图访问它。
Specifically, it means you're accessing data that hasn't been initialized at all. Question -- it this the first attempt to use this structure?
In any case, check your initialization code; when you create the node, I'd bet the very first node, which has no previous, you aren't setting its value, but in the code above you are trying to access it.