title: Function called by pthread_create breaks when a value is assigned to the struct passed to it
body: <p>am brand new to C. I have a little program which is intended to solve find the dot product of a large 2d matrix with itself using pthread. Now when the function assigned to the pthread is called and the struct passed as a variable is accessed, the program breaks and stop working. I don't really know what am doing wrong. Here is the code:</p>
  
  <p><strong>This is the main function.</strong></p>
  
  <pre><code>int main()
  {
      int rc;
      int threadcount = 1;
      char filename[100];
  
      //patch to enable console printing in eclipse
      setvbuf(stdout, NULL, _IONBF, 0);
  
      do {
          prompt_for_fileName(filename);
  
          if (filename[0] == 'Q' || filename[0] == 'q') {
              puts("Program ended");
              return 0;
          }
  
          //read thread count
          read_threadcount(&amp;threadcount);
  
          //initialize matrices
          matrix_def matrix = initialize_matrix(filename);
  
          //get the dimension of sub-matrices
          int dfRow = (int) floor(matrix.NROWS / threadcount);
  
          pthread_t threads[threadcount];
          pthread_arg pthreadargs[threadcount];
          for (int i = 0; i &lt; threadcount; i++) {
              int startRow = i * dfRow;
              int endRow =
                      ((i + 1) == threadcount) ?
                              matrix.NROWS : (startRow + dfRow) - 1; //we're subtracting one because its zero based.
  
              //create a structure that we'll passed to the array.
              pthread_arg arg = { matrix.NROWS, matrix.NCOLS, startRow, endRow,
                      0.0, NULL, NULL };
              arg.data = matrix.data;
              arg.result_set = create_result_memory(matrix.NCOLS);
  
              fprintf(stderr, "before %p\n", arg.result_set);
  
              //push arg into array.
              pthreadargs[i] = arg;
  
              rc = pthread_create(&amp;threads[i], NULL, compute_dot_product,
                      (void *) &amp;arg);
  
              if (rc) {
                  printf("ERROR; return code from pthread_create() is %d\n", rc);
                  exit(-1);
              }
          }
  
          /* Last thing that main() should do */
          pthread_exit(NULL);
  
          puts("Completed processing.");
          double totalTime = 0.0;
          for (int z = 0; z &lt; threadcount; z++) {
              pthread_arg ar = pthreadargs[z];
  
              printf("Thread %d took %g to process %d rows and %d columns.\n", z,
                      ar.execution_time, ar.endz - ar.start, ar.col);
  
              totalTime += ar.execution_time;
          }
  
          printf(
                  "It took the total time of %g, to compute the dot product of the matrices.\n",
                  totalTime);
  
          //free memory
          free(matrix.data);
          for (int k = 0; k &lt; threadcount; k++) {
              free(pthreadargs[k].data);
              free(pthreadargs[k].result_set);
          }
  
      } while (filename[0] != 'Q' || filename[0] != 'q');
  }
  </code></pre>
  
  <p><strong>This is the function being called by the pthread</strong></p>
  
  <pre><code>void * compute_dot_product(void * inputArgs) {
      double startTime, endTime;
      pthread_arg * args = inputArgs;
  
      /*Compute the dimension of the result matrix*/
  
      int col, row, start, endz;
      col = args-&gt;col;
      start = args-&gt;start;
      endz = args-&gt;endz;
      row = endz - start;
  
      fprintf(stderr, "after %p\n", args-&gt;result_set);
  
      //create a pointer to the two array
      double **arr1 = args-&gt;data;
      double **arr2 = arr1;
  
      //begin the computation
      int x;
      startTime = seconds();
  
      //calculate the dot product the two matrices.
      for (x = 0; x &lt; col; x++) {
          double colProduct = 0.0;
          for (int y = start; y &lt; endz; y++) {
              colProduct += arr1[y][x] * arr2[y][x];
          }
  
          //The code breaks here.
          args-&gt;result_set[x] = colProduct;
     }
      endTime = seconds();
  
      double diff = endTime - startTime;
      args-&gt;execution_time = diff;
      return (void *) 4;
  }
  </code></pre>
  
  <p><strong>This is my struct definitions</strong></p>
  
  <pre><code>typedef struct
  {
      int NROWS; /*for m rows*/
      int NCOLS; /*for n columns*/
      double ** data;
  } matrix_def;
  
  typedef struct
  {
      double execution_time;
      matrix_def matrix;
  
  } compute_result;
  
  typedef struct{
      int row;
      int col;
      int start;
      int endz;
      double execution_time;
      double **data;
      double *result_set;
  } pthread_arg;
  </code></pre>
  
  <p><strong>Memory allocation of the 2D matrix.</strong></p>
  
  <pre><code>/*dynamically allocate array based on the read size*/
      matrix.data =  (double **) malloc(sizeof(double *) * M);
  
  
      if(matrix.data != NULL){
          int x;
          for(x = 0; x &lt; M; x++){
              matrix.data[x] = (double) malloc(sizeof(double) * N);
          }
      }else{
          fprintf(stderr, "Unable to allocate memory\n");
          exit(1);
      }
  </code></pre>
  
  <p><strong>Initialize Matrix function</strong></p>
  
  <pre><code>matrix_def initialize_matrix(char *argv)
  {
      int ret_code;
      MM_typecode matcode;
      FILE *f;
      int M, N, nz;
      int i;
      matrix_def matrix;
  
      if((f = fopen(argv, "r")) == NULL)
      {
          fprintf(stderr, "Reading file: '%s' failed", argv);
          exit(1);
      }
  
      /*Read matrix banner*/
      if(mm_read_banner(f, &amp;matcode) != 0)
      {
          printf("Could not process Matrix Market banner. \n");
          exit(1);
      }
  
      /*Check if the current matrix is supported.*/
      if(mm_is_complex(matcode) &amp;&amp; mm_is_matrix(matcode) &amp;&amp; mm_is_sparse(matcode))
      {
          printf("Sorry, this application does not support ");
          printf("Market Matrix type: [%s]\n", mm_typecode_to_str(matcode));
          exit(1);
      }
  
  
      /*find out size of the sparse matrix...*/
      if((ret_code = mm_read_mtx_crd_size(f, &amp;M, &amp;N, &amp;nz)) != 0)
          exit(1);
  
  
      /*Assign m, n sizes.*/
      matrix.NROWS = M;
      matrix.NCOLS = N;
  
      /*dynamically allocate array based on the read size*/
      matrix.data =  (double **) malloc(sizeof(double *) * M);
  
  
      if(matrix.data != NULL){
          int x;
          for(x = 0; x &lt; M; x++){
              matrix.data[x] = (double *) malloc(sizeof(double) * N);
          }
      }else{
          fprintf(stderr, "Unable to allocate memory\n");
          exit(1);
      }
  
  
  
      /*Iterate through the created memory location and fill it with zeros*/
      int a, b;
      for(a = 0; a &lt; M; a++){
          for(b = 0; b &lt; N; b++){
              matrix.data[a][b] = 0;
          }
      }
  
      /*Read the matrix*/
      for(i = 0; i &lt; nz; i++)
      {
          int I = 0, J = 0;
          double val = 0;
          fscanf(f, "%d %d %lg\n", &amp;I, &amp;J, &amp;val);
  
  
          //since the matrix market file starts off at
          //1,1 we have to subtract 1 from the index
          //to account for the array which starts off at
          // 0,0
          matrix.data[--I][--J] = val;
      }
  
      if(f != stdin)
          fclose(f);
  
      return matrix;
  }
  </code></pre>
  
  <p>Any help will be appreciated, as am not very sure with the formula. Thanks</p>