void 指针有条件引发段错误

发布于 2024-11-11 01:17:30 字数 489 浏览 5 评论 0原文

当我们处理空的前一个或下一个指针时,我使用双链表并在边缘情况下获得一些奇怪的性能。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

故事和酒 2024-11-18 01:17:30

大概 prevdl 指向无效内存?拜托,0xdadadada 对您来说真的是一个有效的地址吗? “如果是精神分裂症,那就太白痴了”。

Presumably prevdl points to invalid memory ? Come on, does 0xdadadada really look like a valid address to you ? "It's too idiotic to be schizophrenic".

素罗衫 2024-11-18 01:17:30

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.

楠木可依 2024-11-18 01:17:30

该值:

0xdadadada

看起来与真实的指针值可疑。仅仅因为指针不为 NULL 并不意味着它是有效的。如果你使用无效的指针,你会得到aseg 错误,或者其他什么。

The value:

0xdadadada

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.

没企图 2024-11-18 01:17:30

0xdadadada 看起来有点可疑,也许 prevdl 还没有正确初始化?或者也许它被其他一些代码踩踏了。

也许您应该尝试在 valgrind 下运行测试,看看它是否报告任何内容?

0xdadadada looks a bit suspicious, perhaps prevdl 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?

何以心动 2024-11-18 01:17:30

0xdadadada 听起来不太可能是有效指针的值……很可能是调试初始化程序。

0xdadadada doesn't sound like a very likely value for a valid pointer... most likely a debugging initialiser.

寄意 2024-11-18 01:17:30

具体来说,它意味着您正在访问尚未被访问的数据。完全初始化。问题——这是第一次尝试使用这种结构吗?

无论如何,请检查您的初始化代码;当您创建节点时,我敢打赌第一个节点没有前一个节点,您没有设置它的值,但在上面的代码中您试图访问它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文