0

I try to throw an array of 10 numbers from 1 to 100 to pthread for find min, max and avg number.

I print the values of the array before I throw it.
The values have correct values.

But sometimes it has wrong valves like -209574485 or 65271552.
The wrong values only show up when the array is thrown to pthread.

My code is as follows:

#define N 3
#define MAXSize 10

void *min_thread(void *arg)
{
    int *test = (int*)arg;
    int min;
    for (int i = 1; i <= MAXSize; i++) {
        if (test[i] < min) {
            min = test[i];
        }
    }
    printf("Min number is : %d\n", min);
}

void *max_thread(void *arg)
{
    int *test = (int*)arg;
    int max;
    for (int i = 1; i <= MAXSize; i++) {
        if (test[i] > max) {
            max = test[i];
        }
    }
    printf("Max number is : %d\n", max);
}

void *Avg_thread(void *arg) {
    int *test = (int*)arg;
    int avg;
    for (int i = 1; i <= MAXSize; i++) {
        avg += test[i];
    }
    avg = avg / MAXSize;
    printf("Avg number is : %d\n", avg);
}

int main(int argc, char *argv[])
{
    srand(time(NULL));
    int random_number[MAXSize];
    int min, max, avg;
    for (int i = 0; i < MAXSize; i++) {
        random_number[i] = rand() % 100 + 1;
        printf("%d\n", random_number[i]);
    }
    pthread_t my_thread[N];
    pthread_create(&my_thread[1], NULL, min_thread, (void*)&random_number);
    pthread_create(&my_thread[2], NULL, max_thread, (void*)&random_number);
    pthread_create(&my_thread[3], NULL, Avg_thread, (void*)&random_number);
    pthread_exit(NULL);
    return 0;
}

The result I get from this code:

93
52
72
79
37
96
26
15
86
42
Min number is : -963189760
Avg number is : -96318925
Max number is : 96
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • 2
    your `min`, `max` , and `avg` variables are uninitialized. which means `if (test[i] < min)` , `if (test[i] > max)` will surely lead to uncertain output. – Chirag Acharya May 10 '19 at 13:18
  • 3
    Arrays begin at index `0` btw. – Swordfish May 10 '19 at 13:18
  • regarding: `for (int i = 1; i <= MAXSize; i++) {` In C, the valid index into an array is 0...(number of elements in array - 1) So the posted code is accessing garbage beyond the end of the array (I.E. undefined behavior) Suggest: `for (int i = 0; i < MAXSize; i++) {` – user3629249 May 12 '19 at 06:20
  • regarding: `pthread_exit(NULL); return 0;` The second statement will never be executed because the first statement: `pthread_exit()` will have already exited the function – user3629249 May 12 '19 at 06:23
  • regarding: `pthread_create(&my_thread[1], NULL, min_thread, (void*)&random_number); pthread_create(&my_thread[2], NULL, max_thread, (void*)&random_number); pthread_create(&my_thread[3], NULL, Avg_thread, (void*)&random_number);` the third call to `pthread_create()` has undefined behavior because the only valid indexs to `my_thread[] are: `0, 1,2` so the `3` is accessing beyond the end of the array – user3629249 May 12 '19 at 06:26
  • the proper way to exit a thread function is: `pthread_exit()` which the posted thread functions are failing to do. – user3629249 May 12 '19 at 06:27
  • the main thread should be calling `pthread_join()` for each of the generated threads, before exiting the main thread. Otherwise the whole program can exit before the threads are finished – user3629249 May 12 '19 at 06:28
  • @user463035818 This is C, not C++ They are two different languages – user3629249 May 12 '19 at 06:31
  • when searching for a minimum number, best to initialize the result variable to `INT_MAX` when searching a maximum number, best to initialize the result to `INT_MIN`. When searching for a average number, best to initialize the result to 0 – user3629249 May 12 '19 at 06:32

1 Answers1

2

The problem is that you just created 3 threads but your indexes are beyond the limits. Note that if you define int random_number[10]; then the last element you can access is random_number[9]

The same applies to your min_thread() max_thread() Avg_thread() functions. In each of these functions your for loop is from 1 to 10(the tenth element does not exist), in other words you want to access something which is not in the array. So change

for (int i=1; i<= MAXSize ; i++)

to

for (int i=0; i< MAXSize ; i++)

and

    pthread_create(&my_thread[1], NULL, min_thread, (void*)&random_number);
    pthread_create(&my_thread[2], NULL, max_thread, (void*)&random_number);
    pthread_create(&my_thread[3], NULL, Avg_thread, (void*)&random_number);

to

    pthread_create(&my_thread[0], NULL, min_thread (void*)&random_number);
    pthread_create(&my_thread[1], NULL, max_thread, (void*)&random_number);
    pthread_create(&my_thread[2], NULL, Avg_thread, (void*)&random_number);

As Chirag Acharya suggested, you should also initialize min, max and avg local variables inside the functions. If you do not do that you might get an undefined behavior.

You should take a look at the following link Accessing an array out of bounds gives no error, why? for further details.

Croppi
  • 172
  • 11