Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Wrong pointer inside the function, why?
Forum Updated to NodeBB v4.3 + New Features

Wrong pointer inside the function, why?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 568 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Max_a
    wrote on last edited by
    #1

    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");

    }
    @

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Binary91
      wrote on last edited by
      #2

      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...

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved