title: Thread access static variable cause segmentation error
body: <p>I'm trying to compare performance of methods about adding two matrix.
  one method is just to add.
  the other method is to use threads.</p>
  
  <p>but I have a trouble about segmentation error when a thread access a static variable!</p>
  
  <p>Here is my code.</p>
  
  <p><strong>main.c</strong></p>
  
  <pre><code>#include "matrixProcessor.h"
  
  void main()
  {
  
      time_t s0, e0;
      int i;
      int status;
  
      inputVec1 = (int*)malloc(sizeof(int)*(SIZE*SIZE));
      inputVec2 = (int*)malloc(sizeof(int)*(SIZE*SIZE));
      outputVec = (int*)malloc(sizeof(int)*(SIZE*SIZE));
  
      srand(time(NULL));
  
      initializeVector(inputVec1);
      initializeVector(inputVec2);
  
      //printf("=== INPUT VECTOR 1 ===\n");
      //printVector(inputVec1);
      //printf("=== INPUT VECTOR 1===\n");
      //printVector(inputVec2);
  
      //s0 = clock();
      //addVec(inputVec1, inputVec2, outputVec);
      //e0 = clock();
  
      //printf("Basic Method Time : %f (s)\n",(double)(e0-s0)/CLOCKS_PER_SEC);
  
      s0 = clock();
  
      for(i = 0; i &lt; NUM_THREAD; i++)
      {   
          printf("%d-Thread Call\n",i);
  
          pthread_create(&amp;tid[i], NULL, &amp;addProc, (void*)&amp;i);
          sleep(1);
      }
  
      e0 = clock();
  
      printf("=== OUTPUT VECTOR===\n");
      printVector(outputVec); 
  
      printf("Thread Method Time : %f (s)\n",(double)(e0-s0)/CLOCKS_PER_SEC);
  
      free(inputVec1);
      free(inputVec2);
      free(outputVec);
  
  }
  </code></pre>
  
  <p><strong>matrixProcessor.c</strong></p>
  
  <pre><code>#include "matrixProcessor.h"
  
  
  void initializeVector(int* matPtr)
  {
      int i = 0;
  
      for(i = 0; i &lt; SIZE*SIZE ; i++)
      {
          matPtr[i] = rand()%100;
      }
  }
  
  void addVec(int* inputVec1, int* inputVec2, int* outputVec)
  {
      int i = 0;
  
      for(i = 0; i &lt; SIZE * SIZE; i++)
      {
          outputVec[i] = inputVec1[i] + inputVec2[i];
      }
  }
  
  void* addProc(void *p)
  {
      int* idx = (int*)p;
      int count = (SIZE * SIZE) / NUM_THREAD;
      int i;
  
      printf("idx value : %d\n",*idx);
      printf("Test : %d ", inputVec1[0]);
  
      for(i = (*idx) * count  ; i &lt; (*idx)*count + count; i++)
      {
          outputVec[i] = inputVec1[i] + inputVec2[i];
  
      }
      return NULL;
  }
  
  void printVector(int* vec)
  {
      int i = 0;
      int j = 0;
  
      for(i = 0; i &lt; SIZE ; i++)
      {
          for(j = 0; j &lt; SIZE; j++)
          {
              printf("%d\t", vec[SIZE * i + j]);          
          }
          printf("\n");
      }
  }
  </code></pre>
  
  <p><strong>matrixProcessor.h</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;pthread.h&gt;
  
  #define SIZE 10
  #define NUM_THREAD 10
  
  pthread_t tid[NUM_THREAD];
  
  static int* inputVec1;
  static int* inputVec2;
  static int* outputVec;
  
  void initializeVector(int* matPtr);
  
  void printVector(int* vec);
  void addVec(int* inputVec1, int* inputVec2, int* outputVec);
  void* addProc(void *p);
  </code></pre>
  
  <p>when compiling, i use -static - lpthread options.</p>
  
  <p>I'm sure that accessing static variable cause segmentation error,
  because this program prints some messages before reaching a code line which access static variable.</p>
  
  <p>Here is result.</p>
  
  <pre><code>0-Thread Call
  idx value : 0
  Segmentation Error! ./main
  </code></pre>
  
  <p>Please someone help me..!</p>