Warning: cast to/from pointer from/to integer of different size

前端 未结 4 1203
故里飘歌
故里飘歌 2020-12-14 01:21

I\'m learning Pthreads. My code executes the way I want it to, I\'m able to use it. But it gives me a warning on compilation.

I compile using:

gcc          


        
4条回答
  •  盖世英雄少女心
    2020-12-14 01:57

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

    You are passing the local variable i as an argument for runner, sizeof(void*) == 8 and sizeof(int) == 4 (64 bits).

    If you want to pass i, you should wrap it as a pointer or something:

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

    You may just want i, and in that case, the following should be safe (but far from recommended):

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

提交回复
热议问题