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

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

 #include <unistd.h>
#include <cstdio>
#include <pthread.h> using namespace std; void *thr_fn1(void *arg)
{
printf("thread 1 returning\n");
return (void*);
} void *thr_fn2(void *arg)
{
printf("thread 2 exiting\n");
pthread_exit((void*));
} int main(int argc, char **argv)
{
pthread_t tid1, tid2;
void *tret; pthread_create(&tid1, NULL, thr_fn1, NULL);
pthread_create(&tid2, NULL, thr_fn2, NULL); pthread_join(tid1, &tret);
printf("thread 1 exit code %ld\n", (long)tret); pthread_join(tid2, &tret);
printf("thread 2 exit code %ld\n", (long)tret); return ;
}

既然提示精度损失,那么看一下各自的精度即可:

 #include <iostream>

 using namespace std;

 int main(int argc, char **argv)
{
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(void*) << endl; return ;
}

执行结果:

4
8
8

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

05-12 04:37