title: C++ Segmentation fault on linux
body: <p>Sorry that I am new to c++ and linux, I haven't seen Segmentation fault before. I can successfully compile the file but it just outputs Segmentation fault.</p>
  
  <p>I tried to google Segmentation fault but still no clue on how to fix my code.</p>
  
  <p>Hope someone can rescue me and tell me what's wrong <strong>by specifically stating the line(s) I made errors.</strong>
  I would be grateful about that! Many thanks!</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;time.h&gt;
  #include &lt;cstring&gt;
  #include &lt;semaphore.h&gt;
  
  using namespace std;
  
  #define NUM_THREADS     36
  #define MAX 36
  
  char matrix[6][6]; //result sheet
  
  //queue elements
  char queue[MAX], head=0, tail=0;
  
  
  int row=0,col=0;
  int runtime=1; // track the number of times we have run the dispatch 
  funciton
  
  pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  
  void dispatch(){
  
      row = runtime / 6;
      sleep(1); //wait 1 second to generate column number
      col = runtime % 6 - 1;
      if (runtime % 6 == 0)
          col = 5;
  
      runtime++;
  
  }
  
  
  void enqueue(char c){
      queue[tail] = c;
      tail = (tail+1)%MAX ;
  }
  
  char dequeue(){
      char temp = queue[head];
      head = (head+1)%MAX ;
  return temp;
  }
  
  
  
  
  //master thread
  void *qcTask(void *args) {
  //simulate product convey delay
     sleep(1);
  
     pthread_mutex_lock(&amp;mutex);
  
     dispatch();
  
     sleep(rand()%6+5);
  
     if(rand()%100&lt;90)
         enqueue('Q');
  
     else
         enqueue('U');
  
     pthread_mutex_unlock(&amp;mutex);
  
     pthread_exit(NULL);
  }   
  
  
  
  int main (int argc, char* argv[]){
  
  
     int input = atoi(argv[1]);
      //error handling
     if(argc!=2 || input &lt; 0 || input&gt;36){
        cout&lt;&lt;"Input is illegal!";
     }
  
     else{
         pthread_t threads[input];
  
  
         int rc, i, count = 0;
  
         srand(time(NULL));
  
     for(i = 0;i&lt;input;i++){
  
         rc = pthread_create(&amp;threads[i], NULL, qcTask, NULL);
         if (rc){
             cout &lt;&lt; "Error:unable to create thread," &lt;&lt; rc &lt;&lt; endl;
             exit(-1);
  
       }
     }
  
     for (i = 0; i &lt; input; i++){
         rc = pthread_join(threads[i], NULL);
         if (rc){
             cout &lt;&lt; "Error:unable to join," &lt;&lt; rc &lt;&lt; endl;
             exit(-1);
         }
             }
  
      //initize matrix with default value 'i'
     for (int i = 0; i &lt; 6; i++){
         for (int j = 0; j &lt; 6; j++){
             matrix[i][j] = 'I';
         }
     }
  
     //fill the result sheet
     for(int i=0;i&lt;6;i++){
          for (int j = 0; j &lt; 6; j++){
              matrix[i][j]=dequeue();
          }
  
     }
  
     for (int i = 0; i &lt; 6; i++){
         for (int j = 0; j &lt; 6; j++){
             cout &lt;&lt; matrix[i][j];
             if (matrix[i][j] == 'U')
                 count++; 
         }
         cout &lt;&lt; endl;
     }
  
     cout &lt;&lt; count &lt;&lt; " products are unqualified.";
     pthread_exit(NULL);
  
     }
  }
  </code></pre>