S
S
Silence2019-02-21 16:54:25
C++ / C#
Silence, 2019-02-21 16:54:25

The array is not completely passed to the thread function. Explain what is the problem?

Good evening, please tell me what is my mistake?
I have a task to create an additional thread that sorts an array filled with random numbers (from 1 to m) in the main thread (example on a small number of elements = 5)
If you run the code, the filled array will be displayed first from the main thread, then the transferred array will be displayed to a stream, then a sorted array.
The problem is that the array passed to the stream is sometimes not completely transferred, i. then only 2 elements sometimes all 5 will be transferred. I don’t understand what the problem is ..5c6ead6178acd732415309.png

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
 
using namespace std;
//структура содержащая 2 массива и переменную, которая по сути отвечает за кол-во элементов в массивах
typedef struct structArray
{
        int firstSortedArray[4];
        int secondSortedArray[4];
        int m;
}structArray;
 
//функция сравнения для qsort
int compare (const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}
 
//функция потока
DWORD WINAPI firstSort(LPVOID Param){
    structArray *s=(structArray*)Param;
    for(int i=0;i<s->m;i++)
    cout<< i <<" элемент до сортировки массива:" << s->firstSortedArray[i] << endl;
    
    cout<<"----------После сортировки---------" << endl;
    qsort(s->firstSortedArray,s->m,sizeof(int),compare);
    
    for(int i=0;i<s->m;i++)
    cout<< i <<" элемент после сортировки массива:" << s->firstSortedArray[i] << endl;
return 0;
}
 
int main(){
    setlocale(LC_ALL,"Russian");
    int num;
    
    structArray* s=new structArray;
    s->m=5;
    srand (time(NULL));
    //Заполнение случайными значениям от 1 до м массивов
    for(int i=0;i<s->m;i++){
        num=rand() % s->m +1;
        s->firstSortedArray[i]=num;
        cout<<i<<"элемент массива заполненный в основном потоке:"<<s->firstSortedArray[i] << endl;
        s->secondSortedArray[i]=num;
    }
    cout<<"----------Работа второго потока-------------" << endl;
    DWORD dwThreadId;
    HANDLE hThread = CreateThread(0,0,firstSort,s,0,&dwThreadId);
    WaitForSingleObject(hThread,5000);
    TerminateThread(hThread,dwThreadId);
    
return 1;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2019-02-21
@Adrikk

Array out of bounds here:

for(int i=0;i<s->m;i++){
...
   s->secondSortedArray[i]=num;
}

You have 4 elements in both arrays, and you initialize m to 5. The last assignment to secondSortedArray causes an array overflow, and since after secondSortedArray in the structure there is m, then m is replaced by the last random value.
In firstSortedArray, there is also an array overflow, but this does not have destructive consequences.
Such things often lead to the crash of the program. You could catch it in the debugger. Use it. The debugger is a programmer's friend! :-)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question