?
Solved

Values optimized Out GDB G++

Posted on 2009-04-08
6
Medium Priority
?
1,548 Views
Last Modified: 2013-12-12
I am using GDB in Kdevelop while trying to debug a C++ program compiled with g++.

Many times variables are optimized out by the compiler. However I am using the -O0 option to not optimize the code and have full debugging power. Are there other reasons why some variables are optimized out or switches maybe in Kdevelop???

Thanks.

p.s I have seen the other posts on the -O switch in experts exchange
0
Comment
Question by:PjotterR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 24095139
I assume you use -g (and possibly also -ggdb) to generate debugging information ?

The -O0 switch does indeed mean "no optimization". It is still possible that it performs certain basic optimizations (things like unreachable code for example - see below) however, but it tries to keep the resulting generated code as close as possible to the source code.

Can you give an example of what your compiler optimized at level -O0 ?
#include <stdio.h>
 
int main(void) {
  printf("Before");    /* -O0 : code generated */
  return 0;
  printf("After");     /* -O0 : no code generated (unreachable code) */
}

Open in new window

0
 

Author Comment

by:PjotterR
ID: 24095160
For example in the code below i cannot watch the NoClassObs variable or the Randomnumber. Another problem is that when I step through the code gdb (in kdevelop) seems to randomly jump and even stops and comment lines strangly enough.

          while (CurrentClass != LastClass) {                                
            NoClassObs = (unsigned int)((*CurrentClass).Ratio * PartitionSize);
            for (unsigned int i=0; i < NoClassObs; i++) {                      
              RandomNumber = GetRandomNumber((*CurrentClass).Observations.size()-1);
             CurrentObservation += (RandomNumber);
             (*CurrentPartition).AddObservation(*CurrentObservation);          
             (*CurrentClass).Observations.erase(CurrentObservation);
            }
            CurrentClass++;
          }
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24095167
Maybe you should try running gdb yourself (instead of using the one in kdevelop), and see if that works better.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 35

Accepted Solution

by:
Duncan Roe earned 2000 total points
ID: 24113016
Stopping on comment lines sounds like debugging code older than the source.
"Jumping about" is indeed typical of optimisation - are you sure your development GUI is not overriding your optimisation level on the make command line?
I agree with Infinity08 - compile and debug on the command line so you can be sure you know what's happening
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24178625
Just to add to what duncan_roe has said (and I've been caught out by this before so it can happen) are you sure your latest version of the code actually builds? Are you trying to debug an old version of the compiled code against a newer source. This sounds like such a daft thing to do but I promise, it can and does happen (I feel so silly admitting this :) ).
0
 

Author Closing Comment

by:PjotterR
ID: 31567908
thanks
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question