<div dir="ltr">Hi Marlon,<br><br>Yes it would be necessary to free each char* entry within the char** array<br>prior to freeing the array itself. Please note that this step is only required<br>if the contents of the array are malloc&#39;d. Valgrind and GDB are very useful for<br>debugging memory errors and leaks.<br><br>[0] <a href="http://valgrind.org/">http://valgrind.org/</a><br>[1] <a href="https://www.gnu.org/software/gdb/">https://www.gnu.org/software/gdb/</a><br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Sep 24, 2016 at 9:57 PM, mseaton <span dir="ltr">&lt;<a href="mailto:mseaton@stevens.edu" target="_blank">mseaton@stevens.edu</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
Like argv**, when dynamically creating an array of pointers to string, where each pointer in the array points to a C string of variable length, you have to use malloc twice, one for the array of pointers and one for each string that each array index will point to. Is is absolutely necessary to use free() twice as well?<br>
<br>
for example:<br>
char **filenames;<br>
<br>
filenames = malloc(sizeof(char*) * numfiles);<br>
filenames[i] = malloc( (strlen(direntptr-&gt;d_name) + 1) * sizeof(char));<br>
<br>
Given the two malloc calls above, would I need these two calls to free or is the second call sufficient?<br>
<br>
  for(i = 0; i &lt; numfiles; i++)<br>
     free(filenames[i]);<br>
   free(filenames);<br>
<br>
I am asking this because I am getting a core dump error involving free(). This error is not always occurring. It&#39;s happening on and off.<br>
<br>
Marlon<br>
______________________________<wbr>_________________<br>
cs631apue mailing list<br>
<a href="mailto:cs631apue@lists.stevens.edu" target="_blank">cs631apue@lists.stevens.edu</a><br>
<a href="https://lists.stevens.edu/mailman/listinfo/cs631apue" rel="noreferrer" target="_blank">https://lists.stevens.edu/mail<wbr>man/listinfo/cs631apue</a><br>
</blockquote></div><br></div>