title: Dynamically allocating threads in C
body: <p>I'm creating a little program in C which calculates the faculty of numbers the user enters, until the user enters a negative number. It does this using threads. </p>
  
  <p>I get a segmentation fault when running so I'm doing something wrong, but I don't know what exactly.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>/* 
   * File:   main.c
   * Author: thomasvanhelden
   *
   * Created on June 15, 2014, 3:17 AM
   */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  
  /**
   * Calculates faculty of given number
   *      Can run as a thread
   * @param param The given number
   * @return Doesn't return anything
   */
  void *fac(void *param) {
      int* number = (int*) param;
      int faculty = 1;
      int i; // counter
  
      printf("De faculteit van %i is: ", number);
  
      for (i = 2; i &lt;= number; i++) {
          faculty *= i;
      }
  
      printf("%i\n", faculty);
  }
  
  /*
   * 
   */
  int main(int argc, char** argv) {
      pthread_t **threads; 
      int getal, numThreads = 0, counter, stop = 0;
  
      // ask for numbers until user enters negative number
      while (stop == 0) {
          scanf("%i", getal);
  
          if (getal &gt;= 0) {
              numThreads++;
              threads = realloc(threads, sizeof(pthread_t*) * (numThreads+1));
              threads[numThreads - 1] = malloc(sizeof(pthread_t));
  
              // Create the thread
              if (pthread_create(&amp;threads[numThreads - 1], NULL, fac, &amp;getal)) {
                  // something went wrong
                  printf("Error creating thread %i!\n", numThreads);
                  return 1;
              }
  
          } else {
              // User entered negative number and wants to stop
              stop = 1;
          }
      }
  
      // join all the threads
      for (counter = 0; counter &lt; numThreads; counter++) {
          if (pthread_join(threads[counter], NULL)) {
              printf("Something went wrong joining thread %i\n", counter + 1);
          }
      }
  
  
      return (EXIT_SUCCESS);
  }
  </code></pre>