2007-03-30

Pointer Discrepancy between vc++ & g++


1 #include
2
3 using namespace std;
4
5 int main()
6 {
7 int a = 1, b = 2;
8 int *x;
9
10 cout << "x: " << x << '\t';
11 cout << "*x: " << *x << '\t';
12 cout << "&x: " << &x << '\t';
13
14 cout << endl;
15
16 x = &a;
17 cout << "x = &a" << '\t';
18 cout << "x: " << x << '\t';
19 cout << "*x: " << *x << '\t';
20 cout << "&x: " << &x << '\t';
21
22 cout << endl;
23
24 x = &a - 1;
25 cout << "x = &b" << '\t';
26 cout << "x: " << x << '\t';
27 cout << "*x: " << *x << '\t';
28 cout << "&x: " << &x << '\t';
29
30 cout << endl;
31 return 0;
32 }


Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 (VC6++)


modified code...

x = &a x: 0012FF7C *x: 1 &x: 0012FF74
x = &a-1 x: 0012FF78 *x: 2 &x: 0012FF74


g++ (GCC) 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)


x: 0xb7f518dc *x: -1 &x: 0xbf97f214
x = &a x: 0xbf97f218 *x: 1 &x: 0xbf97f214
x = &a-1 x: 0xbf97f214 *x: -1080561132 &x: 0xbf97f214



g++ 讓我驚訝!! 結果 VC++ 2005 Expression 也讓我受精。 XD

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86



x = &a x: 0012FF60 *x: 1 &x: 0012FF48
x = &a-1 x: 0012FF5C *x: -858993460 &x: 0012FF48

沒有留言: