Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no need to empty it. Often in C, memory allocation is done with <code>malloc</code> which simply returns to you a block of memory which is deemed owned by the caller. When <code>calloc</code> is called, as well as returning you a block of memory, the memory is guaranteed to be initialized to 0. This means for all intents and purposes it is already 'empty'.</p> <p>Also I'm not quite sure if your code does what you are intending. Let me explain what it does at the moment:</p> <pre><code>char arg_array = (char**)calloc(strlen(buf), sizeof (char**)); </code></pre> <p>This line is simply wrong. In C, there is no need to cast pointers returned from <code>calloc</code> because they are of type <code>void *</code>, which is implicitly casted to any other pointer type. In this case, you are storing it in a <code>char</code> type which makes no sense. If you do this:</p> <pre><code>char ** arg_array = calloc(strlen(buf), sizeof (char**)); </code></pre> <p>Then it allocates an array of pointers of <code>strlen(buf)</code> length. So if <code>buf</code> is <code>"hello"</code> then you have now allocated an array which can store 5 pointers.</p> <pre><code>for(i = 0; i&lt;(strlen(buf)); i++) { arg_array[i] = calloc (strlen(buf), sizeof(char*)); } </code></pre> <p>Again, I have removed the redundant cast. What this does is populates the array allocated earlier. Each index of the array now points to a <code>char</code> string of <code>strlen(buf) * sizeof(char *)</code> length. <strong>This is probably not what you want.</strong></p> <hr> <p>Your question is more clear to me now. It appears you want to remove the strings after populating them. You can do it two ways:</p> <ul> <li>Either <code>free</code> each of the pointers and allocate more space later as you did before</li> <li>Or set the first character of each of the strings to a null character</li> </ul> <p>To free the pointers:</p> <pre><code>for(i = 0; i&lt;(strlen(buf)); i++) { free(arg_array[i]); } </code></pre> <p>To set the first character of each string to a null character:</p> <pre><code>for(i = 0; i&lt;(strlen(buf)); i++) { arg_array[i][0] = '\0'; } </code></pre> <p>That is the same code as what you have originally and should be fine.</p> <hr> <p>As proof, the following code will run without errors:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; int main(void) { char * buf = "hello"; char ** arg_array = calloc(strlen(buf), sizeof (char**)); unsigned int i; for(i = 0; i &lt; strlen(buf); i++) { arg_array[i] = calloc(strlen(buf), sizeof(char *)); } for(i = 0; i &lt; strlen(buf); i++) { arg_array[i][0] = '\0'; } for(i = 0; i &lt; strlen(buf); i++) { free(arg_array[i]); } free(arg_array); return EXIT_SUCCESS; } </code></pre> <p>If your code is segfaulting, the problem is coming from somewhere else. Did you overwrite the <code>arg_array</code> variable? Are you sure <code>BUFSIZE</code> is equal to <code>strlen(buf)</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload