title: My code produces junk while uncompressing
body: <p>I've written a simple program to test zlib <code>compress()</code> and <code>uncompress()</code> functions:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  #include "zlib.h"
  
  using namespace std;
  
  int main()
  {
      string str = "Hello Hello Hello Hello Hello Hello!";
      size_t length = str.length();
      size_t newLength = compressBound(length);
      auto compressed = new char[newLength];
      if (compress((Bytef*)compressed, (uLongf*)&amp;newLength, (const Bytef*)str.c_str(), length) != Z_OK)
      {
          throw runtime_error("Error while compressing data");
      }
  
      auto uncompressed = new char[length];
      if (uncompress((Bytef*)uncompressed, (uLongf*)&amp;length, (Bytef*)compressed, newLength) != Z_OK)
      {
          throw runtime_error("Error while uncompressing data");
      }
  
      cout &lt;&lt; uncompressed;
  
      delete[] compressed;
      delete[] uncompressed;
      return 0;
  }
  </code></pre>
  
  <p>Why this program prints something like <code>Hello Hello Hello Hello Hello Hello!¤¤¤¤&amp;У3▒й!</code>? The junk at the end of the string differs from run to run.</p>