title: valgrind - Address ---- is 0 bytes after a block of size 8 alloc'd
body: <p>First, I know <em>similar</em> questions have been asked. However, I'd like to have a more general simple question with really primitive C data types. So here it is.</p>
  
  <p>In  <code>main.c</code> I call a function to populate those string:</p>
  
  <pre><code>int
  main (int argc, char *argv[]){
  
      char *host = NULL ;
      char *database ;
      char *collection_name;
      char *filename = ""; 
      char *fields = NULL;
      char *query = NULL;
      ...
  
      get_options(argc, argv, &amp;host, &amp;database, &amp;collection_name, &amp;filename, 
                  &amp;fields, &amp;query, &amp;aggregation);
  </code></pre>
  
  <p>Inside <code>get_options</code>:    </p>
  
  <pre><code>if (*filename == NULL ) {
     *filename = (char*)realloc(*filename, strlen(*collection_name)*sizeof(char)+4);
      strcpy(*filename, *collection_name);
      strcat(*filename, ".tde");  # line 69 
  }
  </code></pre>
  
  <p>My program works fine, but then Valgrind tells me I'm doing it wrong:
  </p>
  
  <pre class="lang-none prettyprint-override"><code>==8608== Memcheck, a memory error detector
  ==8608== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
  ==8608== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
  ==8608== Command: ./coll2tde -h localhost -d test -c test
  ==8608== 
  ==8608== Invalid write of size 1
  ==8608==    at 0x403BE2: get_options (coll2tde.c:69)
  ==8608==    by 0x402213: main (coll2tde.c:92)
  ==8608==  Address 0xa2edd18 is 0 bytes after a block of size 8 alloc'd
  ==8608==    at 0x4C28BED: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==8608==    by 0x4C28D6F: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==8608==    by 0x403BBC: get_options (coll2tde.c:67)
  ==8608==    by 0x402213: main (coll2tde.c:92)
  </code></pre>
  
  <p>Can you explain the error <code>Address 0xa2edd18 is 0 bytes after a block of size 8 alloc'd</code>?
  How can I solve this issue?</p>