title: Random derangement memory leak
body: <p>I wrote a Secret Santa program that will read a number of game participants, assign each participant to one of the other participants by random derangement, making sure no participant is assigned to themselves, and send each an email.</p>
  
  <p>It seems to execute properly and give intended output, but returns <code>free(): invalid pointer</code> at end of execution before aborting. Am I writing out of bounds somewhere in the <code>derange()</code> function that I can't seem to find? Additionally, does my random derangement logic look sound? It seems to test well, but I have a suspicion it does not give a truly random distribution.</p>
  
  <p>Aware there are functions that could make my life easier like <code>memcpy()</code>, or even <code>shuffle()</code>, but this is mainly a learning exercise for me -- though I do hope to make use of it during the holidays. Be kind, I'm just getting started!</p>
  
  <p><strong>EDIT :</strong> Cleaned up code for more minimal example.</p>
  
  <pre><code>struct person {
      char name[30];
      char emailAddress[30];
      char secretSanta[30];
  };
  
  int main(void) {
      int n;
      scanf("%d", &amp;n);
      struct person * members = (struct person *) malloc(n * sizeof(struct person));
      derange(members, n);
      free(members);
      return 0;
  }
  
  void derange(struct person * p, int n) {
      srand(time(NULL));
      int guesses[n];
      int i, j, r;
      for(i = 0; i &lt; n; i++) {
          r = (rand() % n);
          if(r == i) {
              i = -1;
              // picked its own number, restart loop
          } else {
              for(j = 0; j &lt; n; j++) {
                  if(r == guesses[j]) {
                      i = -1;
                      guesses[j] = -1;
                      // number already used, restart main loop
                  }
              }
              guesses[i] = r;
              for(j = 0; j &lt; 30; j++) {
                  p[i].secretSanta[j] = p[r].name[j];
              }
          }
      }
  }
  </code></pre>