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

  • Home
  • SEARCH
  • 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 6806259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:41:20+00:00 2026-05-26T19:41:20+00:00

In my current project I have a few different interfaces that require me to

  • 0

In my current project I have a few different interfaces that require me to serialize messages into byte buffers. I feel like I’m probably not doing it in a way that would make a true C++ programmer happy (and I’d like to).

I would typically do something like this:

struct MyStruct {
    uint32_t x;
    uint64_t y;
    uint8_t  z[80];
};

uint8_t* serialize(const MyStruct& s) {
    uint8_t* buffer = new uint8_t[sizeof(s)];
    uint8_t* temp = buffer;
    memcpy(temp, &s.x, sizeof(s.x));
    temp += sizeof(s.x);

    //would also have put in network byte order...
    ... etc ...

    return buffer;
}

Excuse any typos, that was just an example off the top of my head. Obviously it can get more complex if the structure I’m serializing has internal pointers.

So, I have two questions that are closely related:

  1. Is there any problem in the specific scenario above with serializing by casting the struct directly to a char buffer assuming I know that the destination systems are in the same endianness?

  2. Main question: Is there a better… erm… C++? way to do this aside from the addition of smart pointers? I feel like this is such a common problem that the STL probably handles it – and if it doesn’t, I’m sure there’s a better way to do it anyway using C++ mechanisms.

EDIT Bonus points if you can do a clean example of serializing this structure in a better way using standard C++/STL without added libraries.

  • 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-05-26T19:41:20+00:00Added an answer on May 26, 2026 at 7:41 pm

    You might want to take a look at Google Protocol Buffers (also known as protobuf). You define your data in a language neutral IDL and then run it through a generator to generate your C++ classes. It’ll take care of byte ordering issues and can provide a very compact binary form.

    By using this you’ll not only be able to save your C++ data but it’ll be useable in other languages (C#, Java, Python etc) as there are protobuf implementation available for them.

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

Sidebar

Related Questions

I have a few classes in my current project where validation of Email/Website addresses
In my current project we have a large repository of content that was originally
I have a spec in my current project that requires us to advise the
I am hosting SpiderMonkey in a current project and would like to have template
I have a short question. Im my current project I'm using LINQ-to-SQl. That is
I have run into another problem with my current project. I have a form
Within my Subversion project I have a few directories that contain other open source
In my current project, I have quite a few objects I need to persist
In my current project I have data about colors. Each color is either a
I have a requirement on my current project (a Flex app which will be

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.