C::B version - 01 oct 2007, gcc 3.4.5, GDB 6.3, WinXP SP2 Pro
I wrote a 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: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.
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.