title: Free() inside a while loop
body: <p>I'm trying to run this program where a character array is created and allocated memory dynamically. Later the array elements are populated with the string "hello" for 10 consecutive locations. The values are assigned to the string elements using strdup() function call.</p>
  
  <p>Once all the elements are assigned the elements are freed up in the while loop.When I run the program in the Visual Studio, the program crashes after the last pointer to the char array is freed. I believe the termination condition for the while loop is correct. But I'm not able to identify what exactly is causing the issue.</p>
  
  <p>Code:</p>
  
  <pre><code>char **p;
  int i;
  
  p = malloc(10 * sizeof(char *));
  for (i = 0; i &lt; 10; i++) {
      p[i] = strdup(“hello”);
  }
  
  while (*p) {
   free(*p++);
  }
  </code></pre>