在CentOS6.2 64位下編譯一下代碼,不經過,提示 ios
./11_2.cpp: In function ‘int main(int, char**)’:
./11_2.cpp:28: 錯誤:從‘void*’到‘int’的轉換損失精度
./11_2.cpp:31: 錯誤:從‘void*’到‘int’的轉換損失精度spa
1 #include <unistd.h> 2 #include <cstdio> 3 #include <pthread.h> 4 5 using namespace std; 6 7 void *thr_fn1(void *arg) 8 { 9 printf("thread 1 returning\n"); 10 return (void*)1; 11 } 12 13 void *thr_fn2(void *arg) 14 { 15 printf("thread 2 exiting\n"); 16 pthread_exit((void*)2); 17 } 18 19 int main(int argc, char **argv) 20 { 21 pthread_t tid1, tid2; 22 void *tret; 23 24 pthread_create(&tid1, NULL, thr_fn1, NULL); 25 pthread_create(&tid2, NULL, thr_fn2, NULL); 26 27 pthread_join(tid1, &tret); 28 printf("thread 1 exit code %ld\n", (long)tret); 29 30 pthread_join(tid2, &tret); 31 printf("thread 2 exit code %ld\n", (long)tret); 32 33 return 0; 34 }
既然提示精度損失,那麼看一下各自的精度便可:指針
1 #include <iostream> 2 3 using namespace std; 4 5 int main(int argc, char **argv) 6 { 7 cout << sizeof(int) << endl; 8 cout << sizeof(long) << endl; 9 cout << sizeof(void*) << endl; 10 11 return 0; 12 }
執行結果:code
4
8
8blog
好吧,確實是精度損失了,從4個字節轉換爲8個字節。可是問題來了,爲何在64位下,指針是8個字節呢?it