gdb、gcc 或两者都有严重错误,我不知道是什么。我创建了一个 POC 来在 gcc (GCC) 4.6.1 20110819(预发布)
和 GNU gdb (GDB) 7.3.50.20110908-cvs
上重现它 - 是的,这个是CVS版本,因为我遇到了这个问题http://sourceware.org/bugzilla/show_bug.cgi?id=12435 ,但是我也使用 GNU gdb (GDB) 7.3.1
进行了测试,它的行为是相同的。
现在到 POC:https://gist.github.com/1211017问题可以在第33行观察到
在我看来,*c->foos[i]
应该求值为第 i 个 foo 的地址,如第 21 行所示。但是可执行文件崩溃了,如您在输出。
有人知道这是怎么回事吗?欢迎 ASM 级别或 GCC 内部的解释。
附录:
- 数据结构不允许更改 - 它们不属于我的代码,我必须与它们进行互操作。
- 代码
*c->foos[i]
用于与旧版本的 gcc 配合使用。 dump_container
必须具有container*
作为参数 - 此函数也不属于我的代码
还值得一提的是:*c->foos[i]
曾经有效。
最佳答案
通过永远有用的 valgrind 运行你的代码说:
==16066== Use of uninitialised value of size 8 ==16066== at 0x40061B: dump_container (bug.c:33) ==16066== by 0x400726: main (bug.c:49) ==16066== ==16066== Invalid read of size 4 ==16066== at 0x400620: dump_container (stdio2.h:105) ==16066== by 0x400726: main (bug.c:49) ==16066== Address 0x89485ed18949ed39 is not stack'd, malloc'd or (recently) free'd ==16066== ==16066== ==16066== Process terminating with default action of signal 11 (SIGSEGV) ==16066== General Protection Fault ==16066== at 0x400620: dump_container (stdio2.h:105) ==16066== by 0x400726: main (bug.c:49) ==16066==
My guess is that you'll find the bug in your own code rather than the compiler or debugger.
EDIT Or since you want me to spell it out for you, change line 33 to
printf("foo[%d]=%d at %p\n", i, (*c->foos)[i]->i, (void*) (*c->foos)[i]);
关于c - 工具链(编译器/调试器)中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7387074/