It is necessary to make a practical one in the S. language. I don’t understand the language at all and somehow it’s badly absorbed by me. Therefore, please help and, if possible, explain.

#include <pthread.h> #include <stdio.h> /* Находим простое число с порядковым номером N, где N — это значение, на которое указывает параметр ARG. */ void* compute_prime(void* arg) { int candidate = 2; int n = *((int*)arg); while (1) { int factor; int is_prime = 1; /* Проверка простого числа путем последовательного деления. */ for (factor = 2; factor < candidate; ++factor) if (candidate % factor == 0) { is_prime = 0; break; } /* Это то простое число, которое нам нужно? */ if (is_prime) { if (--n == 0) /* Возвращаем найденное число в программу. */ return (void*)candidate; } ++candidate; } return NULL; } int main() { pthread_t thread; int which_prime = 5000; int prime; /* Запускаем поток, вычисляющий 5000-е простое число. */ pthread_create(&thread, NULL, &compute_prime, &which_prime); /* Выполняем другие действия. */ /* Дожидаемся завершения потока и принимаем возвращаемое им значение. */ pthread_join(thread, (void*)&prime); /* Отображаем вычисленный результат. */ printf("The %dth prime number is %dn", which_prime, prime); return 0; } 

Knocks error in line 21:

cast-pointer from integer of different size [-Wint-to-pointer-cast] return (void *) candidate;

Tell me how to solve the problem and explain what exactly is the matter?

  • You can resolve the situation by replacing the int candidate = 2; on long candidate = 2; . But the problems of the code are much wider. - αλεχολυτ
  • Thank you, I'm not going to code in the future anyway. I liked PHP and C # more - Andrew

1 answer 1

there are 2 errors

  • return assumes a pointer to candidate, and the code simply indicates candidate. The most obvious answer (albeit incorrect) is to return a pointer — that is, use the '& candidate' (the & sign means to return a pointer to this variable)
  • but there is another nuance, a very "cunning" one - this is already from the use of the stack. Even if you return a pointer to this variable, another error will appear - 'function returns address of local variable'. The fact is that when you declare a variable, it is created on the stack. And when you exit the function, the stack disappears, and the variable goes with it. And trying to access this variable from the outside (when we type the value in main) can output anything. Therefore, you should make this variable not local, but place it in heep. To do this, use malloc - this function "reserves" the memory out of the stack. And after use - free () - this function removes the "reservation". And accordingly, you need to work with this variable through the pointer.

This is what the program will look like.

 #include <stdlib.h> #include <pthread.h> #include <stdio.h> void* compute_prime(void* arg) { int *p_candidate = malloc(sizeof(int)); *p_candidate = 2; int n = *((int*)arg); while (1) { int factor; int is_prime = 1; for (factor = 2; factor < (*p_candidate); ++factor) if (*p_candidate % factor == 0) { is_prime = 0; break; } if (is_prime) { if (--n == 0) return (void*)p_candidate; } ++(*p_candidate); } return &n; return NULL; } int main() { pthread_t thread; int which_prime = 5000; int *prime = NULL; pthread_create(&thread, NULL, &compute_prime, &which_prime); pthread_join(thread, (void*)&prime); if (prime != NULL) { printf("The %dth prime number is %d\n", which_prime, *prime); free (prime); } else { printf ("error"); } return 0; } 

(Alas, I had to remove my comments, because I have problems with the Russian language on the linux)