【问题标题】:pthread_exit() throws warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] warningpthread_exit() 抛出警告:从不同大小的整数转换为指针 [-Wint-to-pointer-cast] 警告
【发布时间】:2021-03-10 08:35:07
【问题描述】:

我一直在学习 pthread 库,并创建了一个简单的代码来将两个数字相乘,但是我无法摆脱这个警告。 附言。代码运行良好。

#include <stdio.h>
#include <pthread.h>

struct numbers {
   int num1,num2;
};

void *mult(void *param) {
   struct numbers *data = param;
   int res = data->num1 * data->num2;
   pthread_exit((void *)res);
}

int main(){

   pthread_t tid;
   struct  numbers n;
   n.num1 = 2;
   n.num2 = 3;
   pthread_create(&tid, NULL,mult, (void*)&n);
   int res;
   pthread_join(tid, (void *)&res);
   printf("%d\n",(int)res);
   return 0;
}

这是警告:

1.c: In function ‘mult’:
1.c:12:17: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
   12 |    pthread_exit((void *)res);

任何见解都将受到高度赞赏。

【问题讨论】:

  • 如果包含&lt;stdint.h&gt;,将res改为intptr_t;它应该关闭你的编译器。你的编译器只是一个卡伦人。

标签: c++ c casting type-conversion pthread-exit


【解决方案1】:

改变

pthread_exit((void *)res);

pthread_exit((void *)&res);

【讨论】:

  • 在原贴代码中res 是自动的;请参阅pthread_exit() 的注释部分
  • 不要那样做,那是完全错误的。如果你幸运的话,你的程序会出错;但通常这只会传播无效数据。
  • @mevets 在这点上是对的。我之前尝试过您的解决方案,尽管它为我们提供了错误的答案,但它消除了警告。
猜你喜欢
  • 2012-03-04
  • 2015-06-04
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
相关资源
最近更新 更多