我正在执行一项使用共享内存的作业,每当运行代码时我都会遇到段错误。所以我尝试使用GDB调试器来查看错误发生在哪里,但是当它运行代码时,它正常退出并说没问题。我粘贴了下面终端中的内容
./main
This is the cb 16669520, This is the dbInfo 2117869600, and this is the db 2117869608
this is the number of processes 1
Segmentation fault (core dumped)
它打印出来的前两行很好,但代码永远不会到达主程序的末尾。然后当我运行调试器时...
gdb ./main
(gdb) run
Starting program: /*path*/
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
This is the cb -8848, This is the dbInfo -243544032, and this is the db -243544024
this is the number of processes 2
The code got this far[Inferior 1 (process 15840) exited normally]
(gdb) backtrace
No stack.
最佳答案
使用valgrind
运行您的程序。它将检测对内存的无效读取和写入,告诉您它们发生在哪里,并且很可能足以帮助您准确地告诉您是哪一行导致了它们。这些错误之一是导致程序出现段错误的原因。
编译程序时不要忘记添加 -ggdb3
标志:)
关于c - 当我运行代码时,出现段错误,但是当调试器运行它时,它说没有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59331873/