title: Conditional jump or move depends on uninitialized value(s) with strcat in for loop
body: <p>I have a file containing the strings of 3 chromosome, which I want to concatenate into one genome. And then I have to access this concatenated string across multiple threads (I use pthread_t). To to this I have to use pthread_mutex_lock when extracting the data, then I use strcat to concatenate the data which are extracted using the function fai_fetch using const char* and then I am saving the data as a char* (see below).</p>
  <pre><code>// genome_size the size of all the chromosomes together
  // chr_total the number of chromosomes I wish to concatenate
  char* genome = (char*) malloc(sizeof(char) * (genome_size+chr_total));
  
  for (int i = 0; i &lt; chr_total; i++){
      pthread_mutex_lock(&amp;data_mutex);
      const char *data = fai_fetch(seq_ref,chr_names[i],&amp;chr_sizes[i]);
      pthread_mutex_unlock(&amp;data_mutex);
      //sprintf(&amp;genome[strlen(genome)],data);
      strcat(genome,data);  
      //sprintf(genome+strlen(genome),data); //All three gives conditional jump or move error
  
      //sprintf(genome,data); // THIS SOLVES VALGRIND ISSUE ONE BUT DOES NOT GIVE A CONCATENATED CHAR*
  }
  </code></pre>
  <p>All of this works, but then running valgrind I get</p>
  <p>Conditional jump or move depends on uninitialized value(s) referring to the  &quot;strcat(genome,data); &quot;</p>
  <p>and Uninitialized value was created by a heap allocation referring to
  &quot;char* genome = (char*) malloc(sizeof(char) * (genome_size+chr_total));&quot;</p>
  <p>Based on other StackOverflow answers I tried sprintf(&amp;genome[strlen(genome)],data); and sprintf(genome+strlen(genome),data); instead of strcat. However they too gives the same valgrind message.</p>
  <p>The only thing that seems to alleviate this error is using sprintf(genome,data); however then i will not get the full genome but just a single chromosome.</p>
  <p>Trying genome += sprintf(genome,data); gives me ./a.out': munmap_chunk(): invalid pointer: and ./a.out': free()</p>
  <p>In regards to the &quot;Uninitialized value was created by a heap allocation&quot; error -&gt; then my issue is that I am only able to free that memory after all of the threads are done running. So I am not sure how to initialize the values in the heap allocation when I am using malloc.</p>
  <p>Is it possible to solve some of these specific valgrind errors?</p>