Author Topic: Debugging: need help  (Read 5270 times)

Offline Jungle

  • Single posting newcomer
  • *
  • Posts: 9
Debugging: need help
« on: October 04, 2007, 07:52:35 am »
C::B version - 01 oct 2007, gcc 3.4.5, GDB 6.3, WinXP SP2 Pro

I wrote a code:
Code
#include <math.h>
#include <iostream>

using namespace std;

int main()
{
  double a = 0, b = 0, c = 0;
  double ha = 0, hb = 0, hc = 0;
  double P = 0, S = 0;

  cout << "Input a, b, c: " << endl;
  cin >> a >> b >> c;

  P  = a + b + c;
  S  = sqrt(P * (P - 2*a) * (P - 2*b) * (P - 2*c)) / 4;
  ha = 2 * S / a;
  hb = 2 * S / b;
  hc = 2 * S / c;

  cout << "Ha = " << ha << "; Hb = " << hb << "; Hc = " << hc << endl;

  return 0;
}

Breakpoint #1 set to line 13 (cin >>...), Breakpoint #2 is on line 15 (P = ...).

When stopped at BP#1, i have the following:
Code
Local variables:
a = 0
b = 0
c = 0
ha = 1.6977949230547824e-313
hb = 1.6266035386702061e-307
hc = 7.3306196620468946e+268
P = -nan(0x8000000000000)
a = 0
b = 0
c = 0
ha = 1.6977949230547824e-313
hb = 1.6266035386702061e-307
hc = 7.3306196620468946e+268

And there is no S variable. When i add new watch for S, the result is the message No Symbol "S" in current context.

BTW, why is it impossible to save whatches if there are only local variables and function arguments?

Well, let a = 3, b = 4, с = 5. Сontinue to the BP#2. Press F7. Expected:
P = 12, next operation - S = ...

Real:
P = 5, next operation - P = ...

and so on... What i'm wrong in? Or incorrect behaviour?

P.S. Program works ok, the problem is in debugging.
« Last Edit: October 04, 2007, 07:54:26 am by Jungle »

Offline denk_mal

  • Multiple posting newcomer
  • *
  • Posts: 35
Re: Debugging: need help
« Reply #1 on: October 04, 2007, 11:04:12 am »
Hello Jungle,

did you have some optimations for code generating turned on?
It seems that gcc uses the same memory for P and S.

(A 'P = S;' right before the return could also result in a 'correct' behavior.)

greetings
denk_mal

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Debugging: need help
« Reply #2 on: October 04, 2007, 12:25:11 pm »
Code
S = sqrt(P * (P - 2*a) * (P - 2*b) * (P - 2*c)) / 4;
S = sqrt(0.0 * (0.0 - 0.0) * (0.0 - 0.0) * (0.0 - 0.0)) / 4.0
S = 0.0/4.0
S = 0.0 (const)


S is practically guaranteed to be replaced by a literal if any optimisations are turned on, and possibly even without any optimisations.

By the way, double a = 0 is not the best thing to do (the same goes for the / 4). Although it works in this case, it has to do an implicit cast, and such things are nasty. One should generally avoid things that require something invisible to happen behind the scene. One day, you'll shoot your foot with this.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7252
Re: Debugging: need help
« Reply #3 on: October 04, 2007, 12:42:04 pm »
The project works fine for me, if optimisation is off.
If optimisation is on, P and S seem to share the same memory, and initialisation with 0 is not done (says gdb).

By the way when I debug without any manually added watches my watches-window is empty. After adding a watch manually, the local and function watches appear, even if I delete the manually added watch.

Offline Jungle

  • Single posting newcomer
  • *
  • Posts: 9
Re: Debugging: need help
« Reply #4 on: October 04, 2007, 01:34:15 pm »
Indeed all seems to be ok when optimization is off.

By the way, double a = 0 is not the best thing to do (the same goes for the / 4). Although it works in this case, it has to do an implicit cast, and such things are nasty. One should generally avoid things that require something invisible to happen behind the scene.

It was just for testing. Anyway i've just started to learn c++, so thank you for the warning.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: Debugging: need help
« Reply #5 on: October 04, 2007, 04:08:23 pm »
You may want to read the example "C programmers learn early on that the result of dividing an integer by an integer is always an integer" on http://www.possibility.com/Cpp/const.html to understand what nasty things can happen.
Things like in this example will happen sooner or later if you are not pedantic about types, and if they happen, you'll spend days or weeks trying to figure it out.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."