when I go through line 22, I still see strange values ββcoming from y, which makes me think that y is incorrectly initialized. but when I go 2 steps further to get into the return line, the number will be correct.
what's going on here? I tried disabling optimization, but this does not work.
1#include <iostream> 2#include <cstring> 3#include <cmath> 4using namespace std; 5 6const int MD = 10; 7struct BigNumber{ 8 int digits[MD]; 15 } 16 17 BigNumber(){ 18 memset( digits, 0, sizeof(digits)); 19 } 20 ***21 BigNumber test(){ 22 BigNumber p,c,y;// on this line , y is seen as garbage 23 p=c;// on this line , y is seen as garbage 24 c=y; 25 //y=p; 26 return y; // on this line, y returned to correct number 27=>}*** 28 29 30 friend ostream& operator<<(ostream& out, const BigNumber& b ); 31}; 32 33ostream& operator<<(ostream& out, const BigNumber& b){ 34 for( int i=MD-1; i>=0; i-- ){ 35 out << b.digits[i]; 36 } 37 return out; 38} 39 40 41 42int main( ){ 43 BigNumber l("4"); 44 BigNumber r; 45 BigNumber p,c,y; 46 47 r=l.test(); 48 cout << r << endl; 49 cout << l.test() << endl; 50 return 0; 51} 28 (gdb) pp $8 = {digits = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} (gdb) pc $9 = {digits = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} (gdb) py $10 = {digits = {1171907, 1235664, -1208027616, 1, 1, 0, 1, 134513360, 134520860, 0}} (gdb) n (gdb) n (gdb) py $11 = {digits = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}