首页 > 解决方案 > gcc 编译器 NULL 分配不分配 NULL

问题描述

我正在调试别人的库(AGAR),我遇到了这样一种情况,即在紧接着的代码行上,指针被分配了一个 NULL 值,它不是 NULL。

非 NULL 值导致段错误!

我在代码中看不到问题。

    int AG_EventLoop(void)
    {
        AG_EventSource *src = AG_GetEventSource();
        AG_EventSink *es;
...
            for((es) = ((&src->epilogues)->tqh_first); (es) != NULL; (es) = ((es)->sinks.tqe_next)) {
                es->fn(es, &es->fnArgs);  // seg fault here should not have entered
            }
...
     }

在 for 循环中,es 被分配了队列中的第一项:

(es) = ((&src->epilogues)->tqh_first);

调试器显示值:

src->epilogues: 0x1bbd28
  tqh_first: 0x0
  tqh_last: 0x1bbd28
es: 0xfeeefeeefeeefeee

如果 tqh_first 为 0x0 (NULL),es 的值 0xfeeefeeefeeefeee 来自哪里?

该代码仅在 MINGW64 中失败,并且在 Linux 中按预期工作。

标签: linuxgccmingw-w64

解决方案


推荐阅读