c - 当我运行代码时,出现段错误,但是当调试器运行它时,它说没有问题

标签 c gdb

我正在执行一项使用共享内存的作业,每当运行代码时我都会遇到段错误。所以我尝试使用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/

相关文章:

c - Posix 线程消费者无法正确打印

c++ - 远程 GDB 检查点/ fork 失败

debugging - 如何在 GDB 中不断观察变量?

c - Linux 缓冲区溢出返回到 libc

c - MinGW 中的消息 "unknown type name ' uint8_t'"

c - 为什么我的随机数生成器仅生成随机数?

c++ - 为什么 cout 不打印 extern "C"变量?

c - pthread_create 中遇到的错误

gdb - 使 gdb 从特定路径加载共享库

assembly - 为什么我在 GDB 上出现断点问题? GDB 停止