title: C - Heap Corruption Detected when freeing array
body: <p>making an assignment and had to dynamically allocate an array of pointers and then free it at the end of the function.
  the problem is when I free the array, it gives me a "Heap Corruption Detected" error and i cant figure out why that happens.
  can anybody see something here ?
  it says im writing after the end of allocated memory but i cant see why.</p>
  
  <pre><code>typedef struct _client
  {
      char id[9];         
      char phone[12]; 
  } Client;
  
  Short_client *createShortClientArr(int n)
  {
      Client *arr = (Client *)malloc(n * sizeof(Client));
      char garbage;
      garbage = getc(stdin);//for getting the '\n' from the last input
      for (int i = 0; i &lt; n; i++)
      {
          fgets(arr[i].id, 9, stdin);
          arr[i].id[9] = '\0';
          garbage = getc(stdin);
          fgets(arr[i].phone, 12, stdin);
          arr[i].phone[12] = '\0';
          garbage = getc(stdin);
      }
  free(arr);
  }
  </code></pre>