Solved

Values optimized Out GDB G++

Posted on 2009-04-08
6
1,505 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 34

Accepted Solution

by:
Duncan Roe earned 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

If your app took Google’s lash recently, here are the 5 most likely reasons.
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
Viewers will learn how to use the Hootsuite Dashboard.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

809 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