Solved

Values optimized Out GDB G++

Posted on 2009-04-08
6
1,490 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
If your app took Google’s lash recently, here are the 5 most likely reasons.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now