How can I declare a variable that should not be optimized (placed in a register) to debug in C ++?

I am developing a simple application in C ++ / Qt and I have the following declaration:

QGridLayout *layout = new QGridLayout;

I am debugging an application using gdb . I set a breakpoint, it works fine, and the debugger gets in line. But if I try to verify 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>"is because the compiler optimized the code and put the data in a register. I am using the g ++ compiler with the checkbox selected -O0(no optimization).

Is there something that I am missing, or is there a way to declare a variable that should not be optimized, say, unlike the storage specifier register? I'm on Ubuntu 10.10 Maverick, kernel 2.6.35-24.

EDIT1

Another 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

For reasons unknown to me, after I compiled with the flag set -v, the error no longer appears, even after it was canceled. Now gdb creates a variable, and I can check its value.

For those interested, compiler flags are set:

g++ -O0 -g3 -Wall -c -fmessage-length=0
+5
source share
3 answers

Use volatile. Perhaps this will help you!

Why do we use the volatile keyword in C ++?

+3

?

Try:

-g -O0 -fno-inline
+3

An easy way to force a variable to pass is to copy the pointer to it into a global variable.

QGridLayout **dummy; // at file scope

//...
dummy = &layout; // in function

This will force the compiler to skip the variable in memory before calling any non-built-in function, since it cannot prove that the function will not access the variable directly.

This may, of course, affect performance, but they are likely to be small compared to the cost of calling GUI member functions in QGridLayout.

+2
source

All Articles