title: How to tell what is causing seg fault when using pthread_create?
body: <p>I'm having trouble finding the cause of this seg fault when calling pthread_create... </p>
  
  <p>GDB is giving me <code>Program received signal SIGSEGV, Segmentation fault.
  0x00007ffff7bc741d in pthread_create@@GLIBC_2.2.5 () from /lib64/libpthread.so.0</code></p>
  
  <p>I'll include the part of the code that calls it and the part of the code which includes the function the threads are calling.</p>
  
  <pre><code>int main(int argc, char **argv)
  {
    /*
     * Declare local variables
     */
    int i, j; // LK
    pthread_t *cat[NUM_CATS]; // LK
    pthread_t *lizard[NUM_LIZARDS]; // LK
  
  
    /*
     * Check for the debugging flag (-d)
     */
    debug = 0;
    if (argc &gt; 1)
      if (strncmp(argv[1], "-d", 2) == 0)
        debug = 1;
  
  
    /*
     * Initialize variables
     */
    numCrossingSago2MonkeyGrass = 0;
    numCrossingMonkeyGrass2Sago = 0;
    running = 1;
  
  
    /*
     * Initialize random number generator
     */
    srandom( (unsigned int)time(NULL) );
  
  
    /*
     * Initialize locks and/or semaphores
     */
  
    sem_init(&amp;driveway, 0, 20); // LK
  
    /*
     * Create NUM_LIZARDS lizard threads
     */
  
    // LK
    for(i = 0; i &lt; NUM_LIZARDS; i++) {
      pthread_create(lizard[i], NULL, lizardThread, (void *)(&amp;i));
    }
  </code></pre>
  
  <p>And the function called by pthread_create...</p>
  
  <pre><code>void * lizardThread( void * param )
  {
    int num = *(int*)param;
  
    if (debug)
      {
        printf("[%2d] lizard is alive\n", num);
        fflush(stdout);
      }
  
    while(running)
      {
  
        lizard_sleep(num); // LK
        sago_2_monkeyGrass_is_safe(num); // LK
        cross_sago_2_monkeyGrass(num); // LK
        lizard_eat(num); // LK
        monkeyGrass_2_sago_is_safe(num); // LK
        cross_monkeyGrass_2_sago(num); // LK
  
      }
  
    pthread_exit(NULL);
  }
  </code></pre>