title: MergeFrom* vs. ParseFrom* in Google's protobuf library
body: <p>I can't seem to find a clear distinction between the <code>MergeFrom*</code> and the <code>ParseFrom*</code> methods of the <a href="https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message_lite#MessageLite" rel="nofollow">MessageLite</a> class in protobuf.</p>
  
  <p>I am trying to minimize the amount of data copying I have to do, so I have written the following code below to decode a length-prefixed-message:</p>
  
  <pre><code>bool StreamMessageDelimiter::receiveWithLengthPrefix(Message&amp; message)
  {
      google::protobuf::uint32 messageSize;
      auto_ptr&lt;google::protobuf::uint8&gt; prefixBuf(new google::protobuf::uint8[sizeof(messageSize)]);
      int receivedBytes = receiveNBytes(prefixBuf.get(), sizeof(messageSize));
      if(receivedBytes != sizeof(messageSize))
      {
          return false;
      }
  
      CodedInputStream prefixInput(prefixBuf.get(), sizeof(messageSize));
      prefixInput.ReadLittleEndian32(&amp;messageSize);
  
      google::protobuf::uint8* payloadBuf = new google::protobuf::uint8[messageSize];
      receivedBytes = receiveNBytes(payloadBuf, messageSize);
      if(receivedBytes != messageSize)
      {
          return false;
      }
  
      ArrayInputStream rawInput(payloadBuf, messageSize);
      CodedInputStream codedInput(&amp;rawInput);
  
      if(!message.MergeFromCodedStream(&amp;codedInput))
      {
          return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>My question is does using the <a href="https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.message_lite#MessageLite.MergeFromCodedStream.details" rel="nofollow">MergeFromCodedStream</a> cause <code>message</code> to take ownership of the <code>payloadBuf</code>, or does <code>message</code> make a copy of the underlying data? If <code>message</code> does indeed make a copy, then I obviously should use an <code>auto_ptr</code> for <code>payloadBuf</code> like I did for the <code>prefixBuf</code>.</p>
  
  <p>Thanks for the input!</p>