title: strdup with stringstreams causes valgrind errors
body: <p>I have the following problem: I am writing a C++ program that has to wrap around a C library, so when I interact with the library I always have to use <code>char*</code> instead of <code>std::string</code> for all operations. In order to avoid working with <code>char*</code> as much as possible, I do the formatting with stringstreams, for example like this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;string.h&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  
  int main(int argc, char** argv)
  {
    ostringstream str;
  
    str &lt;&lt; argv[0] &lt;&lt; "+" &lt;&lt; "hello";
  
    const char *s = str.str().c_str();
  
    char *y = strdup(s);
  
    // this I would give to a library function
  
    cout &lt;&lt; y &lt;&lt; endl;
  
    free(y);
  
    return 0;
  }
  </code></pre>
  
  <p>As far as the output goes, the program correctly outputs "./test+hello". However, valgrind gives me a lot of errors of the type</p>
  
  <pre><code>==30350== Invalid read of size 1
  ==30350==    at 0x402B858: __GI_strlen (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==30350==    by 0x4213475: strdup (in /usr/lib/libc-2.16.so)
  ==30350==    by 0x41B2604: (below main) (in /usr/lib/libc-2.16.so)
  ==30350==  Address 0x4341274 is 12 bytes inside a block of size 25 free'd
  ==30350==    at 0x4029F8C: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
  ==30350==    by 0x410387A: std::string::_Rep::_M_destroy(std::allocator&lt;char&gt; const&amp;) (in /usr/lib/libstdc++.so.6.0.17)
  ==30350==    by 0x41B2604: (below main) (in /usr/lib/libc-2.16.so)
  </code></pre>
  
  <p>What am I doing wrong?</p>