Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:52:33+00:00 2026-06-13T04:52:33+00:00

I can’t seem to find a clear distinction between the MergeFrom* and the ParseFrom*

  • 0

I can’t seem to find a clear distinction between the MergeFrom* and the ParseFrom* methods of the MessageLite class in protobuf.

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:

bool StreamMessageDelimiter::receiveWithLengthPrefix(Message& message)
{
    google::protobuf::uint32 messageSize;
    auto_ptr<google::protobuf::uint8> 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(&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(&rawInput);

    if(!message.MergeFromCodedStream(&codedInput))
    {
        return false;
    }

    return true;
}

My question is does using the MergeFromCodedStream cause message to take ownership of the payloadBuf, or does message make a copy of the underlying data? If message does indeed make a copy, then I obviously should use an auto_ptr for payloadBuf like I did for the prefixBuf.

Thanks for the input!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:52:34+00:00Added an answer on June 13, 2026 at 4:52 am

    Check the documentation for ArrayInputStream:

    “data” remains the property of the caller but must remain valid until
    the stream is destroyed.

    So no, it doesn’t take ownership, and you should make sure to release the memory at the right time.

    I think you might be confused by the use of the word Merge in the function names. It does not come to say “data is merged from the passed buffer” (which I think caused you to consider ownership), but rather “data is merged into the message”. So, Parse calls Clear() before filling the message, whereas Merge uses the message you passed directly as-is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can find why i get this error can someone help? package Android.data; public class
Can anyone help me trying to find out why this doesn't work. The brushes
Can I register a .Net COM class with the SingleUse -flag? The reason I
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you tell me what is the difference between abstraction and information hiding in
Can not find scala.actors package in latest milestones, while it still presents in scaladocs:
Can anyone help me with a little problem I can't solve. I am trying
Can anyone let me know how can we change the value of kendo combobox
Can I order my users in the database, so I don't have to say

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.