Avertissement: fonte/à partir de pointeur à partir de/à l'entier de taille différente

J'apprends Pthreads. Mon code s'exécute comme je le veux, je suis capable de l'utiliser. Mais cela me donne un avertissement sur la compilation.

Je compile en utilisant:

gcc test.c -o test -pthread

Avec GCC 4.8.1. Et je reçois l'avertissement

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;
                      ^

Cette erreur vient pour le code suivant:

#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 > 0n", atoi(argv[1]));
    exit(EXIT_FAILURE);
  }

  if (atoi(argv[1]) > MAX_THREADS) {
    fprintf(stderr,"%d must be <= %dn", atoi(argv[1]), MAX_THREADS);
    exit(EXIT_FAILURE);
  }

  num_threads = atoi(argv[1]);
  printf("The number of threads is %dn", 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=%dn", threadnumber, i);
  pthread_exit(0);
}

Comment puis-je corriger cet avertissement?

24
demandé sur user159 2014-01-24 07:00:01

4 réponses

Un correctif hacky rapide pourrait juste lancer à long au lieu de int. Sur beaucoup de systèmes, sizeof(long) == sizeof(void *).

Une meilleure idée pourrait être d'utiliser intptr_t.

int threadnumber = (intptr_t) param;

Et

pthread_create(&(tid[i]), &attr, runner, (void *)(intptr_t)i);
40
répondu tangrs 2014-01-24 03:12:44
pthread_create(&(tid[i]), &attr, runner, (void *) i);

Vous êtes de passage à la variable locale i comme un argument pour runner, sizeof(void*) == 8 et sizeof(int) == 4 (64 bits).

Si vous voulez passer i, vous devriez l'envelopper comme un pointeur ou quelque chose:

void *runner(void * param) {
  int id = *((int*)param);
  delete param;
}

int tid = new int; *tid = i;
pthread_create(&(tid[i]), &attr, runner, tid);

Vous pouvez i, et dans ce cas, la suite devrait être sûr (mais loin d'être recommandé):

void *runner(void * param) {
  int id = (int)param;
}

pthread_create(&(tid[i]), &attr, runner, (void*)(unsigned long long)(i));
1
répondu Juan Ramirez 2015-02-20 20:08:57

Je recevais aussi le même avertissement. Donc, pour résoudre mon avertissement, j'ai converti int en long et cet avertissement a juste disparu. Et au sujet de l'avertissement "cast de pointeur d'entier de taille différente", vous pouvez quitter cet avertissement, car un pointeur peut contenir la valeur d'une variable, car pointeur en 64x est de 64 bits et en 32x est de 32 bits.

0
répondu newbie 2015-09-17 06:03:03

Essayez de passer

pthread_create(&(tid[i]), &attr, runner, (void*)&i);
-3
répondu venkatesh 2018-03-14 04:44:13