User forums > Using Code::Blocks
Debugging: need help
Jungle:
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;
}
--- End code ---
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
--- End code ---
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.
denk_mal:
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
thomas:
--- Code: ---S = sqrt(P * (P - 2*a) * (P - 2*b) * (P - 2*c)) / 4;
--- End code ---
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.
Jenna:
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.
Jungle:
Indeed all seems to be ok when optimization is off.
--- Quote from: thomas on October 04, 2007, 12:25:11 pm ---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.
--- End quote ---
It was just for testing. Anyway i've just started to learn c++, so thank you for the warning.
Navigation
[0] Message Index
[#] Next page
Go to full version