title: C pthread Segmentation fault
body: <p>so I was trying to make a GPGPU emulator with c &amp; pthreads but ran into a rather strange problem which I have no idea why its occurring. The code is as below:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;assert.h&gt;
  
  // simplifies malloc
  #define MALLOC(a) (a *)malloc(sizeof(a))
  
  // Index of x/y coordinate
  #define x (0)
  #define y (1)
  
  // Defines size of a block
  #define BLOCK_DIM_X (3)
  #define BLOCK_DIM_Y (2)
  
  // Defines size of the grid, i.e., how many blocks
  #define GRID_DIM_X (5)
  #define GRID_DIM_Y (7)
  
  // Defines the number of threads in the grid
  #define GRID_SIZE (BLOCK_DIM_X * BLOCK_DIM_Y * GRID_DIM_X * GRID_DIM_Y)
  
  // execution environment for the kernel
  typedef struct exec_env {
     int threadIdx[2];  // thread location 
     int blockIdx[2];
     int blockDim[2];
     int gridDim[2];
  
     float *A,*B;       // parameters for the thread
     float *C;
  } exec_env;
  
  // kernel 
  void *kernel(void *arg)
  {
      exec_env *env = (exec_env *) arg;
  
      // compute number of threads in a block
      int sz = env-&gt;blockDim[x] * env-&gt;blockDim[y]; 
  
      // compute the index of the first thread in the block
      int k = sz * (env-&gt;blockIdx[y]*env-&gt;gridDim[x] + env-&gt;blockIdx[x]);
  
      // compute the index of a thread inside a block
      k = k + env-&gt;threadIdx[y]*env-&gt;blockDim[x] + env-&gt;threadIdx[x]; 
  
      // check whether it is in range
      assert(k &gt;= 0 &amp;&amp; k &lt; GRID_SIZE &amp;&amp; "Wrong index computation"); 
  
      // print coordinates in block and grid and computed index
      /*printf("tx:%d ty:%d bx:%d by:%d idx:%d\n",env-&gt;threadIdx[x],
                                                env-&gt;threadIdx[y],
                                                env-&gt;blockIdx[x],
                                                env-&gt;blockIdx[y], k);
      */
  
      // retrieve two operands 
      float *A = &amp;env-&gt;A[k];
      float *B = &amp;env-&gt;B[k]; 
      printf("%f %f \n",*A, *B);
      // retrieve pointer to result 
      float *C = &amp;env-&gt;C[k]; 
  
      // do actual computation here !!!
  
      // For assignment replace the following line with 
      // the code to do matrix addition and multiplication.
      *C = *A + *B; 
  
      // free execution environment (not needed anymore)
      free(env);
  
      return NULL;
  }
  
  
  // main function
  int main(int argc, char **argv)
  {
     float A[GRID_SIZE] = {-1}; 
     float B[GRID_SIZE] = {-1};
     float C[GRID_SIZE] = {-1}; 
  
     pthread_t threads[GRID_SIZE]; 
  
     int i=0, bx, by, tx, ty; 
     //Error location
     /*for (i = 0; i &lt; GRID_SIZE;i++){
          A[i] = i;
          B[i] = i+1;
          printf("%f %f\n ", A[i], B[i]);
     }*/
  
     // Step 1: create execution environment for threads and create thread
     for (bx=0;bx&lt;GRID_DIM_X;bx++) {
        for (by=0;by&lt;GRID_DIM_Y;by++) {
           for (tx=0;tx&lt;BLOCK_DIM_X;tx++) {
              for (ty=0;ty&lt;BLOCK_DIM_Y;ty++) { 
  
                 exec_env *e = MALLOC(exec_env);
                 assert(e != NULL &amp;&amp; "memory exhausted"); 
  
                 e-&gt;threadIdx[x]=tx;
                 e-&gt;threadIdx[y]=ty;
  
                 e-&gt;blockIdx[x]=bx;
                 e-&gt;blockIdx[y]=by; 
  
                 e-&gt;blockDim[x]=BLOCK_DIM_X;
                 e-&gt;blockDim[y]=BLOCK_DIM_Y;
  
                 e-&gt;gridDim[x]=GRID_DIM_X;
                 e-&gt;gridDim[y]=GRID_DIM_Y;
  
                 // set parameters
                 e-&gt;A = A; 
                 e-&gt;B = B; 
                 e-&gt;C = C;
  
                 // create thread
                 pthread_create(&amp;threads[i++],NULL,kernel,(void *)e); 
              }
           }
        }
     }
  
     // Step 2: wait for completion of all threads
     for (i=0;i&lt;GRID_SIZE;i++) { 
        pthread_join(threads[i], NULL); 
     }
  
     // Step 3: print result       
     for (i=0;i&lt;GRID_SIZE;i++) { 
        printf("%f ",C[i]); 
     }
     printf("\n");
  
     return 0;
  }
  </code></pre>
  
  <p>Ok this code here runs fine, but as soon as I uncomment the "Error Location" (for loop which assigns A[i] = i and B[i] = i + 1, I get snapped by a segmentation fault in unix, and by these random 0s within C in cygwin. I must admit my fundamentals in C is pretty poor, so it may be highly likely that I missed something. If someone can give an idea on what's going wrong it'd be greatly appreciated. Thanks.</p>