Wrong pointer inside the function, why?



  • Creating thread to sort an arrey.
    When i'm trying to use anything from the struct inside the function i got smt wrong
    @
    struct t_arg // My struct for arguments
    {
    int begin;
    int end;
    } *tmp_struct;

    void * sort(void* arg) //here i got smt wrong
    {
    struct t_arg tmp_arg = *(struct t_arg *) arg;
    int begin = tmp_arg.begin; int end = tmp_arg.end;
    for(int j = begin; j < end; j++)
    for(int i = begin; end-1 < 19; i++)
    {
    if(mass[i] > mass[i+1])
    {
    int tmp = mass[i]; mass[i] = mass[i+1];
    mass[i+1] = tmp;
    } sum = tmp_arg.end;
    }

    return NULL;
    

    }
    void MainWindow::on_pushButton_clicked() //Button Start
    {
    pthread_t thread;
    tmp_struct = new struct t_arg;
    n_qthreads = ui->comboBox->currentText().toInt(); n_e_quantity = ui->spinBox->value();
    mass = new int[n_e_quantity];
    for(int i = 0; i < n_e_quantity; i++)
    {
    mass[i] = rand()0 + 40;
    }
    tmp_struct->begin = 0;
    tmp_struct->end = n_e_quantity;

    pthread_create(&thread, NULL, &sort, &tmp_struct);
    
    ui->textBrowser->insertPlainText(QString::number(tmp_struct->end)); //here always correct
    

    }
    void MainWindow::on_pushButton_3_clicked()
    {
    ui->textBrowser->insertPlainText( "From sort == "); // here i got 0 or smt else, but not end or begin
    ui->textBrowser->insertPlainText(QString::number(sum));
    ui->textBrowser->insertPlainText( "\n\n");

    }
    @



  • They way you are passing the struct to your function is not C++ conform.
    Try the following:
    @
    struct t_arg // My struct for arguments
    {
    int begin;
    int end;
    } *tmp_struct;

    void * sort(t_arg* arg) //here i got smt wrong
    {
    t_arg* tmp_arg = arg;
    //...

    return NULL;
    

    }@

    Also, I don't understand the following in your function MainWindow::on_pushButton_clicked():
    @
    tmp_struct = new struct t_arg;
    // ...
    pthread_create(&thread, NULL, &sort, &tmp_struct); // tmp_struct is already a pointer, so there's no need to reference it...
    @

    I recommend you to check your code and correct your pointer management...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.