[英]How can I declare a variable not to be optimized (put into register) in order to debug in C++?
I'm developing a simple application in C++/Qt , and I have the following declaration: 我正在用C ++ / Qt开发一个简单的应用程序,我有以下声明:
QGridLayout *layout = new QGridLayout;
I'm debugging the application using gdb . 我正在使用gdb调试应用程序。 I set a breakpoint, it works fine, and the debugger hits the line. 我设置了一个断点,它运行正常,调试器就行了。 But if I try to inspect the object declared above I get this output: 但是,如果我尝试检查上面声明的对象,我得到这个输出:
-data-evaluate-expression --thread 1 --frame 0 layout ^done,value="<value> optimized out>"
I read that this message, "<value> optimized out>"
, occurs because the compiler optimized the code, and put the data into the register. 我读到这条消息"<value> optimized out>"
,因为编译器对代码进行了优化,并将数据放入寄存器中。 I'm using g++ compiler, with flag -O0
(none optimization) set. 我正在使用g ++编译器,设置标志-O0
(无优化)。
Is there something I'm missing, or does it exist a way to declare a variable not to be optimized, say, as opposed to the storage specifier register
? 有没有我缺少的东西,或者它是否存在一种声明变量不被优化的方式,比如存储说明符register
? I'm on Ubuntu 10.10 Maverick, kernel 2.6.35-24. 我在Ubuntu 10.10 Maverick,内核2.6.35-24。
EDIT1 EDIT1
Some more code: 更多代码:
WorkspaceChooserDialog::WorkspaceChooserDialog(QWidget *parent) : QDialog(parent)
{
setWindowTitle(tr("Select a workspace location"));
QLabel *wpLabel = new QLabel(tr("Workspace:"), this);
QLineEdit *wpLineEdit = new QLineEdit(QDir().homePath(), this);
QPushButton *okButton = new QPushButton(tr("OK"), this);
QPushButton *cancelButton = new QPushButton(tr("Cancel"), this);
QGridLayout *layout = new QGridLayout;
connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
qDebug() << "begin: " << layout << " :end";
layout->addWidget(wpLabel, 0, 0);
layout->addWidget(wpLineEdit, 0, 1, 1, 2);
layout->addWidget(okButton, 1, 1);
layout->addWidget(cancelButton, 1, 2);
setLayout(layout);
}
EDIT2 EDIT2
For reasons unknown to me, after I compiled with verbose mode -v
flag set, the error didn't appear anymore, even after unsetting it again. 由于我不知道的原因,在使用详细模式-v
标志设置编译之后,错误不再出现,即使再次取消设置后也是如此。 Now gdb creates the variable, and I'm able to inspect its value. 现在gdb创建变量,我能够检查它的值。
For who are interested, the compiler's flags set are: 对于谁感兴趣,编译器的标志集是:
g++ -O0 -g3 -Wall -c -fmessage-length=0
Use volatile
. 使用volatile
。 Maybe, it'll help you! 也许,它会帮助你!
Why do we use volatile keyword in C++? 为什么我们在C ++中使用volatile关键字?
What other compiler flags have you set? 您设置了哪些其他编译器标志?
Try: 尝试:
-g -O0 -fno-inline
An easy way to force a variable to be allocated is to copy a pointer to it to a global variable. 强制分配变量的一种简单方法是将指向它的指针复制到全局变量。
QGridLayout **dummy; // at file scope
//...
dummy = &layout; // in function
This will force the compiler to spill the variable to memory before calling any non-inlined function, as it cannot prove that the function would not access the variable directly. 这将强制编译器在调用任何非内联函数之前将变量溢出到内存,因为它无法证明函数不会直接访问变量。
This may, of course, have performance impacts, but they're likely to be small compared to the cost of calling the GUI member functions on QGridLayout. 当然,这可能会对性能产生影响,但与在QGridLayout上调用GUI成员函数的成本相比,它们可能会很小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.