title: Segmentation Fault in pthreads, Linux Ubuntu
body: <p>I'm getting a Segmentation Fault when I run this code. Surprisingly, when I set <code>thread_count</code> to 16 or less, it doesn't give any error. When I debug the code using gdb, the code gets an error at line <code>local_answer += vec_1[j] * vec_2[j];</code> in the <code>Calculate()</code> thread function. What is the reason for this behavior? How can I fix that?</p>
  
  <p>I'm compiling with this gcc command:</p>
  
  <pre><code>gcc test.c -o DP -lpthread -lm -mcmodel=large -g
  </code></pre>
  
  <p>And here's the code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/time.h&gt;
  #include &lt;time.h&gt;
  #include &lt;math.h&gt;
  #include &lt;pthread.h&gt;
  
  double *vec_1 = NULL;
  double *vec_2 = NULL;
  int vec_length = 0;
  int thread_count = 0;
  double answer = 0;
  double *partial_results = NULL;
  pthread_mutex_t mutex;
  
  void *Calculate(void *arg) {    
      int myId = (int) arg;
      int myStart = myId * vec_length / thread_count;
      int myEnd = (myId + 1) * vec_length / thread_count;
  
      double local_answer = 0;
      int j;
  
      for(j = myStart; j &lt; myEnd; j++) {
          local_answer += vec_1[j] * vec_2[j];
      }
      pthread_mutex_lock(&amp;mutex);
      partial_results[myId] = local_answer;
      pthread_mutex_unlock(&amp;mutex);
  }
  
  int main(int argc, const char *argv[]) {    
      srand((unsigned int) time(NULL)); 
      pthread_mutex_init(&amp;mutex, NULL);
  
      int num_iterations = 5;
  
      vec_length = 1000000000;  
      thread_count = 25;
  
      partial_results = (double*) malloc(thread_count * sizeof(double)); 
  
      double avg_time = 0;
      int i;
  
      vec_1 = (double*) malloc(vec_length * sizeof(double)); 
      vec_2 = (double*) malloc(vec_length * sizeof(double));
  
      if(vec_1==NULL || vec_2==NULL){
          printf("Memory Allocation failed");
          exit(0);
      }
  
      int j;
      for (j = 0; j &lt; vec_length; j++) {
          vec_1[j] = ((double) rand() / (double) (RAND_MAX)) + 1;
          vec_2[j] = ((double) rand() / (double) (RAND_MAX)) + 1;             
      }
  
      for (i = 0; i &lt; num_iterations; i++) {
          pthread_t threads[thread_count];
          pthread_attr_t attr;
          void* status;       
          struct timeval t1, t2;
          gettimeofday(&amp;t1, NULL);
  
          pthread_attr_init(&amp;attr);
          pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE);
  
          int t;
          for (t = 0; t &lt; thread_count; t++) {            
              if (pthread_create(&amp;threads[t], NULL, Calculate, (void*)(t))) {
                  printf("ERROR in pthread_create()");
                  exit(-1);
              }
          }
  
          pthread_attr_destroy(&amp;attr);
  
          answer = 0;
          for (t = 0; t &lt; thread_count; t++) {            
              if (pthread_join(threads[t], &amp;status)) {
                  printf("ERROR in pthread_join()");
                  exit(-1);
              }
              answer += partial_results[t];
          }
  
          gettimeofday(&amp;t2, NULL);     
          avg_time += (t2.tv_sec - t1.tv_sec) * 1000.0 + (t2.tv_usec - t1.tv_usec) / 1000.0;
      }
  
      printf("Average time Spent : %lf \n", avg_time / num_iterations);
  
      pthread_mutex_destroy(&amp;mutex);
      return 0;
  }
  </code></pre>