title: Valgrind, "uninitialized value(s)" error
body: <p>In my C program, I'm allocating memory using <code>malloc()</code> which does, in contrast to <code>calloc()</code>, <em>not</em> initialize the memory and it might still contain garbage. Mostly, in context of the allocation, I do not make any changes to the memory allocated by <code>malloc()</code>. (For example in a function to initialize a struct that contains a buffer, I do not make changes to the buffer's memory, but later on).</p>
  
  <p>Valgrind gives me a lot of theese errors:</p>
  
  <ul>
  <li><em>Conditional jump or move depends on uninitialised value(s)</em></li>
  <li><em>Use of uninitialised value of size 4</em></li>
  </ul>
  
  <p>I <em>am sure</em> to never read from memory that was not initialized in these cases.</p>
  
  <p>Should I ignore them or is it better to initialize the memory on allocation? In case I should ignore them, how can I deactivate this error message in Valgrind?</p>
  
  <hr>
  
  <p><strong>Example 1</strong>:</p>
  
  <pre><code>==4253== Conditional jump or move depends on uninitialised value(s)
  ==4253==    at 0x408EB8E: vfprintf (vfprintf.c:1624)
  ==4253==    by 0x4093C2E: printf (printf.c:35)
  ==4253==    by 0x40624D2: (below main) (libc-start.c:226)
  ==4253==  Uninitialised value was created by a heap allocation
  ==4253==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==4253==    by 0x8048938: gk_StreamBufferNode_init (stream.c:101)
  ==4253==    by 0x8048D0D: gk_Stream_bufferWriteProc (stream.c:252)
  ==4253==    by 0x8048665: main (main.c:21)
  </code></pre>
  
  <p><em>Code</em>:</p>
  
  <pre><code>int gk_StreamBufferNode_init(gk_StreamBufferNode* node, int buffer_size,
                               gk_AllocProc malloc) {
      node-&gt;buffer = malloc(buffer_size);     // line 101
      if (node-&gt;buffer == NULL) {
          return GKIT_FAILEDALLOC;
      }
      node-&gt;next = NULL;
      return GKIT_NOERR;
  }
  </code></pre>
  
  <hr>
  
  <p><strong>Example 2</strong>:</p>
  
  <pre><code>==4253== Conditional jump or move depends on uninitialised value(s)
  ==4253==    at 0x402DA39: memcpy (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==4253==    by 0x8048C6E: gk_Stream_bufferWriteProc (stream.c:230)
  ==4253==    by 0x8048665: main (main.c:21)
  ==4253==  Uninitialised value was created by a heap allocation
  ==4253==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==4253==    by 0x8048CE0: gk_Stream_bufferWriteProc (stream.c:248)
  ==4253==    by 0x8048665: main (main.c:21)
  </code></pre>
  
  <p><em>Code</em>:</p>
  
  <pre><code>    /* ... */
      int available_bytes = binfo-&gt;buffer_size - bnode-&gt;filled;
      int bytes_to_go = size * count;
      int offset = 0;
      int node_offset = 0;
      gk_StreamBufferNode* new_node;
      void* destination = NULL;
      void* source = NULL;
  
      while (bytes_to_go &gt; 0) {
          destination = bnode-&gt;buffer + bnode-&gt;filled + node_offset;
          source = buffer + offset;
          if (available_bytes &gt; bytes_to_go) {
              memcpy(destination, source, bytes_to_go);    // line 230
              bnode-&gt;filled += bytes_to_go;
              offset += bytes_to_go;
              node_offset = bytes_to_go;
              bytes_to_go = 0;
          }
          else {
              memcpy(destination, source, available_bytes);
              offset += available_bytes;
              node_offset = 0;
              bytes_to_go -= available_bytes;
              bnode-&gt;filled += available_bytes;
  
              #ifdef DEBUG
                  assert(bnode-&gt;filled == bnode-&gt;buffer_size);
              #endif // DEBUG
  
              // Allocate a new buffer node.
              new_node = (gk_StreamBufferNode*) malloc(sizeof(gk_StreamBufferNode));    // line 248
              if (new_node == NULL) {
                  return GKIT_FAILEDALLOC;
              }
              int success = gk_StreamBufferNode_init(new_node, binfo-&gt;buffer_size,
                                                     malloc);
              if (success &lt;= GKIT_ERROR) {
                  free(new_node);
                  return GKIT_FAILEDALLOC;
              }
              bnode-&gt;next = new_node;
              bnode = new_node;
              available_bytes = binfo-&gt;buffer_size;
          }
      }
  </code></pre>