title: Parsing a string in C with strsep (alternative methods)
body: <p>I want to parse a string, and I use <code>strsep</code> function:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  int main()
  {
  
    char str[] = "Marco:Q:2F7PKC";
    char *token1, *token2, *token3;
    char *r = malloc(30);
  
    strcpy(r, str);
  
    token1 = strsep(&amp;r, ":");
    token2 = strsep(&amp;r, ":");
    token3 = strsep(&amp;r, ":");
  
    printf("tok1 = %s\n", token1);
    printf("tok2 = %s\n", token2);
    printf("tok3 = %s\n", token3);
  
    free(r);
  
    return 0;
  
  }
  </code></pre>
  
  <p>The function do its job well, but If I launch <strong>valgrind</strong>, the allocated string <code>char * r</code> does not freed correctly (<strong>definitely lost: 30 bytes in 1 blocks</strong>).</p>
  
  <p>I'd like to know why and if there are alternative way to do the same thing, maybe without call <em>strsep</em>.</p>
  
  <p>I call valgrind with <code>valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./a.out</code></p>