Предупреждение: отбрасывание в/из указателя от/до целого разного размера
Я изучаю 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);
}
Как я могу исправить это предупреждение?
Ответы
Ответ 1
Быстрое хакерское исправление может просто привести к long
вместо int
. На множестве систем sizeof(long) == sizeof(void *)
.
Лучшей идеей может быть использование intptr_t
.
int threadnumber = (intptr_t) param;
и
pthread_create(&(tid[i]), &attr, runner, (void *)(intptr_t)i);
Ответ 2
pthread_create(&(tid[i]), &attr, runner, (void *) i);
Вы передаете локальную переменную i
в качестве аргумента для runner
, sizeof(void*) == 8
и sizeof(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 в длинный, и тогда это предупреждение просто исчезло. И о предупреждении "отбрасывать на указатель из целого разного размера" вы можете оставить это предупреждение, потому что указатель может удерживать значение любой переменной, поскольку указатель в 64x имеет 64-битный, а в 32x - 32 бит.