title: Segmentation fault in multithreaded program
body: <p>I have declared a struct and tried to use the elements of that struct in the main program . I am not sure if it is the right approach. what can be an alternative approach . Could it be declaring new matrices and variables in main and then assigning those values to the values of struct . I am getting all 0s in first row of resultant matrix but second row is computed correctly.Also , should I create threads dynamically.</p>
  
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  struct v {
  int i; /* row */
   int j; /* column */
    int M, N, K;
  float** A;
  float** B;
  float** C;
  };
  
  
   void *runner(void *param); /* the thread */
  
    int main(int argc, char *argv[]) {
  
    int c, d, k, sum = 0;
   pthread_t tid[50];       //Thread ID
    int i,j, count = 0,thread_identifier,ret=5, count_dimension=0;
     char ch;
      struct v *data = (struct v *) malloc(sizeof(struct v));
  
      printf("Enter the number of rows and columns of first matrix\n");
      scanf("%d%d", &amp;(data-&gt;M), &amp;(data-&gt;K));
       (data-&gt;A) = malloc((data-&gt;M)*sizeof(float*));
       for ( i =0;i &lt; data-&gt;M; i++)
   (data-&gt;A[i]) =malloc((data-&gt;K)*sizeof(float));
  
     printf("Enter the number of rows and columns of second matrix\n");
     (data-&gt;B) = malloc((data-&gt;K)*sizeof(float*));
     scanf("%d%d", &amp;(data-&gt;K), &amp;(data-&gt;N));
      for( i=0;i&lt;(data-&gt;K);i++)
   (data-&gt;B[i]) = malloc((data-&gt;N)*sizeof(float));
  
       printf("Allocate memory for result matrix \n\n");
      (data-&gt;C) = malloc((data-&gt;M)*sizeof(float*));
       for( i=0;i&lt;(data-&gt;M);i++)
       (data-&gt;C[i]) = malloc((data-&gt;N)*sizeof(float));
  
        printf("Enter the elements of first matrix\n");
        for( i = 0 ; i &lt; data-&gt;M ; i++ )
         for ( j = 0 ; j &lt; data-&gt;K ; j++ )
         scanf("%f", &amp;(data-&gt;A[i][j]));
  
          printf("Enter the elements of second matrix\n");
          for( i = 0 ; i &lt; data-&gt;K ; i++ )
           for ( j = 0 ; j &lt; data-&gt;N ; j++ )
            scanf("%f", &amp;(data-&gt;B[i][j]));
  
  
  
           for(i = 0; i &lt; data-&gt;M; i++) 
    {
       j=0;
  
       data-&gt;i = i;
       data-&gt;j = j;
       /* Now create the thread passing it data as a parameter */
  
       pthread_attr_t attr; //Set of thread attributes
       //Get the default attributes
       pthread_attr_init(&amp;attr);
       //Create the thread
       pthread_create(&amp;tid[i],&amp;attr,runner,data);
   //printf("create worker thread %u for row %d",(unsigned int)pthread_self(),i);
   printf("%lu",tid[i]);   
   printf("\n");
       count++;
   }  
  
     for(i=0;i&lt; data-&gt;M;i++)
    {         
  
  //Make sure the parent waits for all threads to complete
       pthread_join(tid[i], NULL);
     }      
  
     //Print out the resulting matrix
      for(i = 0; i &lt; data-&gt;M; i++) {
      for(j = 0; j &lt; data-&gt;N; j++) {
       printf("%.2e ", data-&gt;C[i][j]);
    }
  printf("\n");
  
    }
    printf("\n"); 
    printf("%d",count);
   }
  
    //The thread will begin control in this function
     void *runner(void *param) {
     struct v *data = param; // the structure that holds our data
     int n,x=0, j=0; //the counter and sum
    float sum = 0.00;
     for(x=0;x&lt;data-&gt;N;x++)
    {
  for(j=0;j&lt;data-&gt;N;j++)
  {
      sum += data-&gt;A[data-&gt;i][j] * data-&gt;B[j][x];
  }
  data-&gt;C[data-&gt;i][x] = sum;
  sum=0.00;
     }
  
    //Exit the thread
    //pthread_exit(0);
      }
  </code></pre>