title: segFault on return from pthread_join
body: <p>I am implementing a stack pool to do some basic arithmetic in a specific number of threads.</p>
  <p>here is the code:</p>
  <pre class="lang-c prettyprint-override"><code>int add(int a, int b) {return a+b;}
  int sub(int a, int b) {return a-b;}
  int mul(int a, int b) {return a*b;}
  sem_t sem_ops;
  void* compute(void* arg){
      Args *args = arg;
      Operation* op = args-&gt;operation;
      printOP(*op);
      int * value = malloc ( sizeof ( int ) ) ;
      * value = op-&gt;op(op-&gt;a, op-&gt;b);
      args-&gt;is_complete = true;
      sem_post(&amp;sem_ops);
      return value;
  }
  void push(Operation op , Queue* q){
      q-&gt;operations[++(q-&gt;last)] = op;
  }
  Operation *pop(Queue *q){
      q-&gt;first++;
      return &amp;q-&gt;operations[q-&gt;first];
  }
  int size(Queue *q){
      return q-&gt;last - q-&gt;first;
  }
                 
  bool read_operations(char* filename, Queue* ops){
      printf(&quot;reading from file %s\n&quot;,filename);
      FILE *fp = fopen(filename, &quot;r&quot;);
      if(fp == NULL ) 
      {
          printf(&quot;couldnt open&quot;);
          return false;
      }
      int id,a,b;
      while(!feof(fp)){
          fscanf(fp,&quot;%d %d %d\n&quot;,&amp;id,&amp;a,&amp;b);
          Operation op;
          op.a = a;
          op.b = b;
          switch (id){
              case 0:
                  op.op = add;
                  break;
              case 1:
                  op.op = sub;
                  break;
              case 2:
              default:
                  op.op =mul;
                  break;
          }
          push(op,ops);
      }
      return true;
  }
  ArrayList* execute_thread_pool(char* filename, int poolSize){
      sem_init(&amp;sem_ops,0,poolSize);
      //init return value
      ArrayList* retval =malloc(sizeof(ArrayList));
      int res [ TOTAL_OP_COUNT];
      retval-&gt;results = res;
      retval-&gt;count = 0;
      // populate Q
      Queue q;
      Operation ops[TOTAL_OP_COUNT];
      q.first = 0;
      q.last = 0;
      q.max = TOTAL_OP_COUNT;
      q.operations = ops;
      read_operations(filename,&amp;q);
      // thread tids
      pthread_t threads[TOTAL_OP_COUNT];
      //args for each thread
      Args args[TOTAL_OP_COUNT];
      int* result= NULL;
      for(int i =0; i&lt;poolSize; ++i){
          sem_wait(&amp;sem_ops);
          args[i].operation = pop(&amp;q);
          args[i].is_complete=false;
          pthread_create(&amp;threads[i],NULL, compute,&amp;args[i]);
      }
      for(int i =0; i&lt;poolSize; ++i){
          if(args[i].is_complete){
              pthread_join(threads[i],(void**) result);
              res[i] = *result; // line 88
          }else --i;
      }
      return retval;
  }
  </code></pre>
  <p>and the structs typedefs:</p>
  <pre><code>typedef struct _Operation{
      int(*op) (int, int);
      int a;
      int b;
  } Operation;
  typedef struct _Args{
      Operation* operation;
      bool is_complete;
  }Args;
  typedef struct _Queue{
      Operation* operations;
      int last;
      int first;
      int max;
  }Queue;
  </code></pre>
  <p>I tried using malloc at a few different places, but can't run away from segfault.
  this is the content of the main function:</p>
  <pre class="lang-c prettyprint-override"><code>    ArrayList *al =malloc(sizeof(ArrayList)) ;
      al = execute_thread_pool(&quot;bob.txt&quot;, 4);
  </code></pre>
  <p>in gdb the bt is as follow:</p>
  <pre><code>#0  0x0000555555555762 in execute_thread_pool (filename=0x555555556004 &quot;bob.txt&quot;, poolSize=4) at pool.c:88
  #1  0x00005555555552db in main (argc=1, argv=0x7fffffffda68) at main.c:5
  </code></pre>
  <p>I've been on this for a few hours, tried multiple ways, tried without semaphores and using not so fun loops and yet the same result. can anyone help me spot the issue?</p>