【问题标题】:Warning: cast to/from pointer from/to integer of different size警告:从/到不同大小的整数转换为/从指针转换
【发布时间】:2014-02-14 21:55:11
【问题描述】:

我正在学习 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_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);
}

如何解决此警告?

【问题讨论】:

    标签: gcc int void-pointers gcc-warning


    【解决方案1】:

    一个快速的hacky修复可能只是转换为long而不是int。在很多系统上,sizeof(long) == sizeof(void *)

    一个更好的主意可能是使用intptr_t

    int threadnumber = (intptr_t) param;
    

    pthread_create(&(tid[i]), &attr, runner, (void *)(intptr_t)i);
    

    【讨论】:

    • 或在 64 位机器/编译器上转换为 long long
    • @thejinx0r 当然,但从某种意义上说,它仍然是一个 hack,你不能依赖 sizeof(long long) == sizeof(void *)
    • @NikhilWagh,你不能总是依赖sizeof(size_t) == sizeof(void *)
    • 在 64 位机器上没有什么对我有用。在作为参数传递给线程函数之前,我必须声明 long 类型的变量。 long i; pthread_create(&amp;pid[i], NULL, displayThread, (void *)i);
    【解决方案2】:
    pthread_create(&(tid[i]), &attr, runner, (void *) i);
    

    您将 局部变量 i 作为 runnersizeof(void*) == 8sizeof(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,在这种情况下,以下应该是安全的(但远不推荐):

    void *runner(void * param) {
      int id = (int)param;
    }
    
    pthread_create(&(tid[i]), &attr, runner, (void*)(unsigned long long)(i));
    

    【讨论】:

      【解决方案3】:

      我也收到了同样的警告。因此,为了解决我的警告,我将 int 转换为 long,然后这个警告就消失了。关于“从不同大小的整数转换为指针”的警告,您可以留下此警告,因为指针可以保存任何变量的值,因为 64x 中的指针是 64 位的,而 32x 中的指针是 32 位的。

      【讨论】:

        【解决方案4】:

        尝试通过

        pthread_create(&(tid[i]), &attr, runner, (void*)&i);
        

        【讨论】:

          猜你喜欢
          • 2012-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-04
          • 2019-08-13
          相关资源
          最近更新 更多