title: sbrk - Valgrind doesn't report the memory leak
body: <p>I wrote this little version of <code>malloc</code> (no <code>free</code>):</p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;cstdio&gt;
  #include &lt;cstddef&gt;
  #include &lt;unistd.h&gt;
  
  #define word_size sizeof(intptr_t)
  #define align(n) ((n + word_size - 1) &amp; ~(word_size - 1))
  
  void* my_malloc(size_t size) {
      void* p = sbrk(0);
      printf("before allocation: %p\n", p);
      if (sbrk(align(size)) == (void*) -1) {
          // failed to allocate memory
          return NULL;
      }
      printf("after allocation: %p\n", sbrk(0));
      return p;
  }
  
  int main() {
      int* foo = (int*) my_malloc(1);
      *foo = 100;
      printf("after allocation outside: %p\n", sbrk(0));
  }
  </code></pre>
  
  <p>Here is the output of the code:</p>
  
  <pre><code>before allocation: 0x1796000
  after allocation: 0x1796008
  after allocation outside: 0x1796008
  </code></pre>
  
  <p>As you can see, the memory allocated by <code>my_malloc</code> has not been freed.
  Yet when I run it through <code>valgrind</code> with this:</p>
  
  <pre><code>valgrind --leak-check=yes ./main
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>==1592== Memcheck, a memory error detector
  ==1592== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
  ==1592== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
  ==1592== Command: ./main
  ==1592== HEAP SUMMARY:
  ==1592==     in use at exit: 0 bytes in 0 blocks
  ==1592==   total heap usage: 2 allocs, 2 frees, 73,728 bytes allocated
  ==1592== 
  ==1592== All heap blocks were freed -- no leaks are possible
  ==1592== 
  ==1592== For counts of detected and suppressed errors, rerun with: -v
  ==1592== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
  </code></pre>
  
  <p>As you can see, <code>valgrind</code> didn't find <em>any</em> memory leaks! Am I using <code>valgrind</code> incorrectly, or is this a bug?</p>