title: Are there downsides to using std::string as a buffer?
body: <p>I have recently seen a colleague of mine using <code>std::string</code> as a buffer:</p>
  
  <pre><code>std::string receive_data(const Receiver&amp; receiver) {
    std::string buff;
    int size = receiver.size();
    if (size &gt; 0) {
      buff.resize(size);
      const char* dst_ptr = buff.data();
      const char* src_ptr = receiver.data();
      memcpy((char*) dst_ptr, src_ptr, size);
    }
    return buff;
  }
  </code></pre>
  
  <p>I guess this guy wants to take advantage of auto destruction of the returned string so he needs not worry about freeing of the allocated buffer.</p>
  
  <p>This looks a bit <strong>strange</strong> to me since according to <a href="http://www.cplusplus.com/reference/string/string/data/" rel="noreferrer">cplusplus.com</a> the <code>data()</code> method returns a <code>const char*</code> pointing to a buffer internally managed by the string:</p>
  
  <pre><code>const char* data() const noexcept;
  </code></pre>
  
  <p><strong>Memcpy-ing to a const char pointer</strong>? AFAIK this does no harm as long as we know what we do, but have I missed something? Is this dangerous?</p>