title: C - Freeing string after cycle for
body: <p>I have this simple function for extrapolate a substring in a string.</p>
  
  <pre><code>char* substr(const char *string, size_t start, size_t end) {
  
    const char *char_start = &amp;string[start];
    const char *char_end = &amp;string[end];
  
    char *substring = (char *) calloc(1, char_end - char_start + 1);
    memcpy(substring, char_start, char_end - char_start + 1);
  
    return substring;
  } 
  </code></pre>
  
  <p>I have only one <code>calloc</code>, that create the returned string. 
  I try the code in a cycle, for extrapolate the substring of a string array.
  This is the main code where I test the function:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(void) {
    size_t i;
    char *tmp = NULL;
    char *kmer_array[5] = {"GTGAA", "ACGGT", "AACGG", "AGTGA", "TGAAC"};
  
    for ( i = 0; i &lt; 5; i++ ) {
      tmp = substr(kmer_array[i], 1, strlen(kmer_array[i]));
    }  
  
    free(tmp);      
  
    return 0;
  } 
  </code></pre>
  
  <p>But when I test the code with valgrind this is the output (<a href="https://pastebin.com/yyAG3kJV" rel="nofollow noreferrer">link</a>).</p>
  
  <p>I dont't understade where I lost the byte</p>