首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >警告:传递“”pthread_join“”的参数1将使指针中的整数不会发生强制转换错误

警告:传递“”pthread_join“”的参数1将使指针中的整数不会发生强制转换错误
EN

Stack Overflow用户
提问于 2015-06-27 05:39:35
回答 2查看 5.4K关注 0票数 2
代码语言:javascript
运行
复制
#include  pthread.h
#include stdio.h

static int seq[50];
void *runner(void *); /* the thread */

int main(int argc, char *argv[])
{
    int y;
    pthread_t tid[3]; /* the thread identifier */
    pthread_attr_t attr; /* set of attributes for the thread */
    if(argc!=2)
    {
        fprintf(stderr,"you need to enter two arguments");
        exit(-1);
    }
    else
    {
        int a = atoi(argv[1]);
        if(a>0)
        {
            y=a;
        }
        else
        {
            fprintf(stderr,"you need to enter a valid number greater than zero");
            exit(-1);
        }
    }
    /* get the default attributes */
    pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
    /* create three threads */
    pthread_create(&tid, NULL, runner,(void *)y);
    /* now wait for the thread to exit */
    sleep(2);
    printf( " Am in main process, The sequence is  ");
    int j=0,val=0;
    for( j = 0; j < 40; j++)
    {
        val=seq[j];
        if(val>=1)
            printf( "  %d  ", seq[j]);
    }
    pthread_join(tid, NULL);
    pthread_exit(0);
}
/**
 * The thread will begin control in this function
 */
void *runner(void *param)
{
    int count=0;
    int y= (int *) param;
    //       printf(" Am in runner");
    while(y!=1)
    {
        if(y%2==0)
            y = y/2;
        else
            y= ((3*y)+1);
        //  printf(" %d ", y);
        seq[count] = y;
        count++;

    }
    pthread_exit(0);
}

我得到了以下错误

代码语言:javascript
运行
复制
bonus.c: In function ‘main’:
bonus.c:91: warning: cast to pointer from integer of different size
bonus.c:91: warning: passing argument 1 of ‘pthread_create’ from incompatible pointer type
bonus.c:123: warning: passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast
bonus.c: In function ‘runner’:
bonus.c:157: warning: comparison between pointer and integer
bonus.c:159: error: invalid operands to binary %
bonus.c:161: error: invalid operands to binary /
bonus.c:165: error: invalid operands to binary *
bonus.c:171: warning: assignment makes integer from pointer without a cast
EN

回答 2

Stack Overflow用户

发布于 2015-06-27 07:12:32

无论出于什么原因,您都要声明一个pthread_t数组

代码语言:javascript
运行
复制
pthread_t tid[3];

不要这样做,除非你打算启动三个线程。

你想要的

代码语言:javascript
运行
复制
pthread_t tid;

有两个错误是由此引起的:

代码语言:javascript
运行
复制
pthread_create(&tid, NULL, runner,(void *)y);
(passing argument 1 of ‘pthread_create’ from incompatible pointer type)

其中,传递指向数组的指针而不是指向pthread_t的指针,以及

代码语言:javascript
运行
复制
pthread_join(tid, NULL);
(passing argument 1 of ‘pthread_join’ makes integer from pointer without a cast)

其中传递指向pthread_t而不是pthread_t的指针。

警告

代码语言:javascript
运行
复制
cast to pointer from integer of different size

来自(void*) y -如果您正在为64位目标进行编译,则intvoid*更小。

你应该使用&y或者给y一个和void*一样大的类型,比如int64_t

runner中,您将参数强制转换为int*,这非常奇怪,因为它开始时是一个int

您需要确保将类型转换回开始时的相同类型。

然后使用这个值初始化一个int,但这不可能是您的实际代码,因为以下错误与

代码语言:javascript
运行
复制
int* y = (int *) param;

因此,我怀疑您在输入问题中的代码时丢失了一个"*“。

如果在参数中传递指向int的指针,则应为

代码语言:javascript
运行
复制
int y = *(int*) param;

如果您传递一个重新解释为指针的整数,它应该是

代码语言:javascript
运行
复制
int64_t y = (int64_t) param;

如果你选择int64_t作为你的整型。

此外,请记住,在线程修改数组元素时打印它们可能不是最好的想法,也不能确保线程不会超出数组边界。

票数 3
EN

Stack Overflow用户

发布于 2015-06-27 05:42:43

代码语言:javascript
运行
复制
pthread_create(&tid, NULL, runner,(void *)y);

==>

代码语言:javascript
运行
复制
for( i = 0; i < 3; i++){
    pthread_create(&tid[i], NULL, runner,(void *)y);
}    

代码语言:javascript
运行
复制
pthread_join(tid, NULL);

==>

代码语言:javascript
运行
复制
for( i = 0; i < 3; i++){
    pthread_join(tid[i], NULL);
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31082559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档