Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think there is a bug memory leakage because you first allocate </p> <pre><code> char **words = (char **)malloc(sizeof(char *) * (size +1)); //when declaring </code></pre> <p>when declaring the variable, and after that you again allocate the same <code>**words</code> in the loop body:</p> <pre><code> char **words = (char **)malloc(sizeof(char *) * (size +1)); // in the while loop </code></pre> <p>The above line in the while loop with which you allocate the space to store the string should be (1)</p> <pre><code> //in the while loop should be char *words[index] = (char *)malloc(sizeof(char ) * (size +1)); strcpy (words[index], str); </code></pre> <p>Or simply (2)</p> <pre><code> words[index] = str; </code></pre> <p>Because the <code>str</code> already points to a valid memory location which you assign to the array of pointers.</p> <p>In the (1) method above you are allocating a block of memory of <code>size+1</code> of type <code>char</code> and copying the string in <code>str</code> into <code>words[index]</code> with <code>strcpy</code>. For this you require to reserve a memory location into <code>words[index]</code> first and then perform the copy. If this is the case the the memory freeing is not at simple as <code>free (word)</code> instead each of the allocated block will need to be manually removed.</p> <pre><code> for (index = 0; words[index] != 0; index++) { free (words[index]; } free (words); </code></pre> <p>In the (2) solution is in my opinion not a good one, because you have passed a pointer to a string and assign that pointer value to store the string. So both the <code>str</code> and the <code>words[index]</code> point to the same location. Now after the function returns if anybody <code>free</code>s <code>str</code> (if it was dynamically allocated) then the <code>words[index]</code> reference will become illegal.</p> <p>EDIT:</p> <p>Also you need to use</p> <p><code>gets (words);</code> or in using c++ <code>cin &gt;&gt; words;</code> or use <a href="http://www.cprogramming.com/tutorial/string.html" rel="nofollow">getline</a>, or simply increment the <code>index</code> counter in your code, and assign a null at the end to terminate the string.</p> <p>in main function. You do not increment the <code>index</code> counter so all the characters are assigned in the same location.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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