简体   繁体   English

为了在C ++中调试,如何声明一个不被优化的变量(放入寄存器)?

[英]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.

相关问题 我如何查看一个值<optimized out> C++ 中的变量?</optimized> - How do I view the value of an <optimized out> variable in C++? 如何在C ++ Builder 6中将SQL计数放入变量中? - How can i put a SQL count in a variable in C++ Builder 6? 我如何在 C++ 中声明 Class - how can i declare a Class in C++ 我如何声明一个变量,使其保留其值并可以从C ++中的任何类访问? - How can I declare a variable so that it will hold its value and can be accessible from any class in C++? 如何在 c++ 中声明一个相同的 class 但具有不同模板参数的新变量? - How can I declare a new variable of the same class but with different template parameters in c++? 我可以在C ++类中将成员变量声明为const吗? - Can i declare member variable as const in class of c++?if yes,how? 如何在C ++ Qt中的函数中声明要使用的变量? - How do I declare a variable to use in a function in C++ Qt? 如何在不使用 std 的情况下将索引按降序排列在 C++ 中? - How can I put the indexs in descending order in C++ without using std? 在 Windows 64 位中,如何将 .asm 文件中的寄存器值“放入”C++ 变量中? - In Windows 64bits, how to "put" a register value from an .asm file, in a C++ variable? 我如何信任声明const的C ++函数的行为? - How can I trust the behavior of C++ functions that declare const?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM