Link to home
Start Free TrialLog in
Avatar of Member_2_4548968
Member_2_4548968

asked on

Values optimized Out GDB G++

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
Avatar of Infinity08
Infinity08
Flag of Belgium image

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

Avatar of Member_2_4548968
Member_2_4548968

ASKER

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++;
          }
Maybe you should try running gdb yourself (instead of using the one in kdevelop), and see if that works better.
ASKER CERTIFIED SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 :) ).
thanks