How do I view the value of an lt;optimized outgt; variable in C++?(如何查看 lt;optimized outgt; 的值?C++中的变量?)
问题描述
我正在使用 gdb 调试 C++ 程序.
I am using gdb to debug a C++ program.
我有这个代码:
int x = floor(sqrt(3));
我想查看 x 的值.但是,gdb 声称 x 是
and I want to view the value of x. However, gdb claims that x is "< optimized_out >". How do I view the value of x? Should I change my compiler flags?
推荐答案
在高优化级别上,编译器可以消除中间值,正如您在此处看到的.有多种选择:
On high optimization levels, the compiler can eliminate intermediate values, as you have seen here. There are a number of options:
- 您可以降低优化级别,使调试器更容易跟踪事物.
-O0
肯定可以工作(但会慢很多),-O1
也可以正常工作. - 您可以添加一些显式打印语句来记录输出值.
- 您通常也可以强制编译器通过将其设置为 volatile 来保留此特定值(但请记住在完成后将其取消设置为 volatile!).但是请注意,由于控制流在优化代码中也会发生变化,即使您可以看到变量的值,当您查看有问题的变量.
- You can reduce the optimization level to make it easier for the debugger to keep track of things.
-O0
is certain to work (but will be quite a lot slower),-O1
might work okay as well. - You can add some explicit print statements to log the output value.
- You can also usually force the compiler to retain this specific value by making it volatile (but remember to un-make it volatile when you're done!). Note, however, that since control flow is also subject to alteration in optimized code, even if you can see the value of the variable, it may not be entirely clear what point in the code you're at when you're looking at the variable in question.
这篇关于如何查看 <optimized out> 的值?C++中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何查看 <optimized out> 的值?C++中的变量


基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01