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 7705139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:48:51+00:00 2026-05-31T23:48:51+00:00

I’ve got a few files that have been serialized by directly writing C++ struct

  • 0

I’ve got a few files that have been serialized by directly writing C++ structs using a binary fstream. Now, when I generate the read methods for corresponding C# classes, I find out that these don’t really map 1-to-1, probably because of structure packing.

So my question is – what is the best way to handle the situation? I’ve got both C++ and C# source code, so I can change either part. Should I attempt to serialize C++ without packing (I assume this is done with #pragma pack(1), right) or instead somehow adjust my C# code to take account of the gaps?

Or is the problem with deserializing completely different and nothing to do with packing?

Update 1 I discovered that C++ enums are 4 bytes long and bool values are 2 bytes long (who knew?), but even after adding #pragma pack(1) I am only able to correctly read the first record. Also, I’ve checked the number of bytes against sizeof(MyStructure) and the values match. This must mean that there is some padding between the records.

Update 2 Darn, found a bug in code. Copy-paste error. Need PVS Studio or something. All good now. (And yeah, a boolean is 1 byte big.)

  • 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-31T23:48:52+00:00Added an answer on May 31, 2026 at 11:48 pm

    Alignment is the likely cause of the problem.

    Structure packing/byte alignment is compiler implementation dependent, and also varies with the build target to optimise for the hardware in question. This means it would be wise not to rely on a given set of structure alignments that your compiler happens to produce for a given build.

    If you need to share structures reliably, it would be best to #pragma pack(1) and add the padding back in manually if performance is an issue. This is only likely to be a concern if the structures are used in a tight inner loop.

    An example of manual alignment:

    struct Record {
        char c;
        char __padding[7];
        double d;
    };
    

    Your C# code would then look something like:

    this.c = reader.ReadChar();
    reader.ReadBytes(7); // or reader.BaseStream.Seek(7, SeekOrigin.Current);
    this.d = reader.ReadDouble();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,

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.