GDB prints wrong values(GDB 打印错误的值)
问题描述
I've got a bit of a problem with debugging a C++ program using GDB.
When I use print object.member, it doesn't always print the value of the variable correctly. Instead, it prints the value of one of the arguments to the function I'm debugging. And it doesn't change through the function, although I change the value of object.member throughout.
And the thing is, the program is rather large and consists of several modules, with partially specialised templates and such, so I can't post it all here.
Now I tried to create a minimal testcase, but whatever simple I tried, I can't make it work. I mean, not work.
So all I can ask is, has anybody ever seen this behaviour in GDB, and have you found out what caused it and how to solve it?
There are question here about similar behaviour, but those amount to the program not being compiled properly (optimisation levels too high etc). I compiled it with -Wall -Wextra -pedantic -g -O0, so that can't be it.
And the program runs fine; I can cout << object.member; and that outputs the expected value, so I don't know what to try now.
I've seen similar behaviour before. Unfortunately, gdb is really 'C' based so although it will deal with C++, I've found it occasionally to be quite picky about displaying values.
When displaying more complex items (such as maps, strings or the dereferenced contents of smart pointers) you have to sometimes be quite explicit about dereferencing and casting variables.
Another possibility is the function itself - anything unusual about it? Is it templated for example?
Can you create a reference to this variable in your code and try displaying that? Or take the address of the variable and derefrence the contents - only if it's publicly available of course.
Naturally the source code must match what you've compiled so must be older than the exe but gdb will normally warn you about such things
这篇关于GDB 打印错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:GDB 打印错误的值
基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
