Gcc 警告:将指针从/转换为不同大小的整数
我正在学习Pthreads。我的代码按照我希望的方式执行,我能够使用它。但它给了我一个关于编译的警告 我使用以下方法编译:Gcc 警告:将指针从/转换为不同大小的整数,gcc,int,void-pointers,gcc-warning,Gcc,Int,Void Pointers,Gcc Warning,我正在学习Pthreads。我的代码按照我希望的方式执行,我能够使用它。但它给了我一个关于编译的警告 我使用以下方法编译: gcc test.c -o test -pthread 根据GCC 4.8.1。我得到了警告 test.c: In function ‘main’: test.c:39:46: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] pthread_creat
gcc test.c -o test -pthread
根据GCC 4.8.1。我得到了警告
test.c: In function ‘main’:
test.c:39:46: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
pthread_create(&(tid[i]), &attr, runner, (void *) i);
^
test.c: In function ‘runner’:
test.c:54:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
int threadnumber = (int) param;
^
以下代码出现此错误:
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#define MAX_THREADS 10
int sum; /* this data is shared by the thread(s) */
void *runner(void * param);
int main(int argc, char *argv[])
{
int num_threads, i;
pthread_t tid[MAX_THREADS]; /* the thread identifiers */
pthread_attr_t attr; /* set of thread attributes */
if (argc != 2) {
fprintf(stderr, "usage: test <integer value>\n");
exit(EXIT_FAILURE);
}
if (atoi(argv[1]) <= 0) {
fprintf(stderr,"%d must be > 0\n", atoi(argv[1]));
exit(EXIT_FAILURE);
}
if (atoi(argv[1]) > MAX_THREADS) {
fprintf(stderr,"%d must be <= %d\n", atoi(argv[1]), MAX_THREADS);
exit(EXIT_FAILURE);
}
num_threads = atoi(argv[1]);
printf("The number of threads is %d\n", num_threads);
/* get the default attributes */
pthread_attr_init(&attr);
/* create the threads */
for (i=0; i<num_threads; i++) {
pthread_create(&(tid[i]), &attr, runner, (void *) i);
printf("Creating thread number %d, tid=%lu \n", i, tid[i]);
}
/* now wait for the threads to exit */
for (i=0; i<num_threads; i++) {
pthread_join(tid[i],NULL);
}
return 0;
}
/* The thread will begin control in this function */
void *runner(void * param)
{
int i;
int threadnumber = (int) param;
for (i=0; i<1000; i++) printf("Thread number=%d, i=%d\n", threadnumber, i);
pthread_exit(0);
}
#包括
#包括
#包括
#定义最多10个线程
整数和;/*此数据由线程共享*/
void*runner(void*param);
int main(int argc,char*argv[])
{
int num_线程,i;
pthread_t tid[MAX_THREADS];/*线程标识符*/
pthread\u attr\u t attr;/*线程属性集*/
如果(argc!=2){
fprintf(stderr,“用法:测试”);
退出(退出失败);
}
if(atoi(argv[1])最大线程数){
fprintf(stderr,“%d”必须为空
您正在传递局部变量i
作为runner
,sizeof(void*)==8
和sizeof(int)==4
(64位)的参数
如果要传递i
,应将其包装为指针或其他内容:
void *runner(void * param) {
int id = *((int*)param);
delete param;
}
int tid = new int; *tid = i;
pthread_create(&(tid[i]), &attr, runner, tid);
您可能只需要i
,在这种情况下,以下内容应该是安全的(但远不是推荐的):
一个快速的黑客修复程序可能只是转换为long
,而不是int
。在许多系统上,sizeof(long)==sizeof(void*)
更好的办法可能是使用intptr\t
int threadnumber = (intptr_t) param;
及
我也收到了同样的警告。所以为了解决我的警告,我将int转换为long,然后这个警告就消失了。关于警告“从不同大小的整数转换为指针”“您可以保留此警告,因为指针可以保存任何变量的值,因为64x中的指针是64位的,32x中的指针是32位的。尝试传递。”
pthread_create(&(tid[i]), &attr, runner, (void*)&i);
或者在64位计算机/编译器上转换为long-long
。@thejinx0r当然可以,但它仍然是一种黑客行为,因为你不能依赖sizeof(long-long)==sizeof(void*)
@NikhilWagh再次强调,你不能总是依赖sizeof(size\t)==sizeof(void*)
。在64位计算机上,我什么也做不到。在作为参数传递给线程函数之前,我必须声明long类型的变量。long I;pthread\u create(&pid[I],NULL,displayThread,(void*)I);
int threadnumber = (intptr_t) param;
pthread_create(&(tid[i]), &attr, runner, (void *)(intptr_t)i);
pthread_create(&(tid[i]), &attr, runner, (void*)&i);