title: How can I declare a variable not to be optimized (put into register) in order to debug in C++?
body: <p>I'm developing a simple application in <strong>C++/Qt</strong>, and I have the following declaration:</p>
  
  <p><code>QGridLayout *layout = new QGridLayout;</code></p>
  
  <p>I'm debugging the application using <strong>gdb</strong>. 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:</p>
  
  <blockquote>
  <pre><code>-data-evaluate-expression --thread 1 --frame 0 layout 
  ^done,value="&lt;value&gt; optimized out&gt;"
  </code></pre>
  </blockquote>
  
  <p>I read that this message, <code>"&lt;value&gt; optimized out&gt;"</code>, occurs because the compiler optimized the code, and put the data into the register. I'm using <strong>g++</strong> compiler, with flag <code>-O0</code> (none optimization) set.</p>
  
  <p>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 <code>register</code>? I'm on Ubuntu 10.10 Maverick, kernel 2.6.35-24.</p>
  
  <p><strong>EDIT1</strong></p>
  
  <p>Some more code:</p>
  
  <pre><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() &lt;&lt; "begin: " &lt;&lt; layout &lt;&lt; " :end";
      layout-&gt;addWidget(wpLabel, 0, 0);
      layout-&gt;addWidget(wpLineEdit, 0, 1, 1, 2);
      layout-&gt;addWidget(okButton, 1, 1);
      layout-&gt;addWidget(cancelButton, 1, 2);
      setLayout(layout);
  }
  </code></pre>
  
  <p><strong>EDIT2</strong></p>
  
  <p>For reasons unknown to me, after I compiled with verbose mode <code>-v</code> flag set, the error didn't appear anymore, even after unsetting it again. Now gdb creates the variable, and I'm able to inspect its value.</p>
  
  <p>For who are interested, the compiler's flags set are:</p>
  
  <pre><code>g++ -O0 -g3 -Wall -c -fmessage-length=0
  </code></pre>