从‘void*’到‘int’的转换损失精度

在CentOS6.2 64位下编译一下代码,不通过,提示 

./11_2.cpp: In function ‘int main(int, char**)’:
./11_2.cpp:28: 错误:从‘void*’到‘int’的转换损失精度
./11_2.cpp:31: 错误:从‘void*’到‘int’的转换损失精度

 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 }

执行结果:

4
8
8

好吧,确实是精度损失了,从4个字节转换为8个字节。但是问题来了,为什么在64位下,指针是8个字节呢?

posted @ 2014-11-24 17:27  冷冰若水  阅读(5261)  评论(0编辑  收藏  举报