Solved

Values optimized Out GDB G++

Posted on 2009-04-08
6
1,519 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Event log and black screen 22 69
awk file 6 102
Supermicro 91 error 8 62
mounting drive a in a linux 14 39
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
In this article, you will read about the trends across the human resources departments for the upcoming year. Some of them include improving employee experience, adopting new technologies, using HR software to its full extent, and integrating artifi…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 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