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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:52:29+00:00 2026-05-19T16:52:29+00:00

I want to grab part of the data inside some C structs to partially

  • 0

I want to grab part of the data inside some C structs to partially serialize/deserialize them, writing the bytes from memory to disk, and viceversa.

The structs are not known in advance, they are dynamically built with my own C code generator (as well as the code that will serialize it). The serializable fields will be placed at the beginning of the struct.

Let’s say a have a struct with 4 fields, and the first two are to be serialized:

typedef struct {
   int8_t x1;   
   int32_t x2;   /* 1 + 4  = 5 bytes (if packed) */
   int8_t y1;
   int32_t  y2;   /* 1 + 4  +1 + 4 = 10 bytes (if packed) */ 
}  st;

I plan to grab the pointer to the struct variable and write/read the n bytes that cover those two first fields (x1, x2). I don’t think I need to worry about alignment/packing because I don’t intend the serialization to survive different compilations (only a unique executable is expected to read/write the data). And, as I’m targeting a wide scope of compilers-architectures, I don’t want to place assumptions on alignment-packing or compiler specific tricks.

Then, I need to count bytes. And I can’t just do sizeof(st.x1)+sizeof(st.x2) because of alingment-padding. So, I’m planning to substract pointers, from the start of the struct to the first “non persistent” field:

st myst;
int partsize = (char*)&myst.y1 - (char*)(&myst);
printf("partial size=%d (total size=%d)\n",partsize,sizeof(myst));  

This seems to work. And it can be placed in a macro.

(For the record: I tried also to write another macro that does not requrire an instance of the struct, something like this, but it doesnt seem possible here – but this does not matter me much).

My question: Is this correct and safe? Can you see any potential pitfall, or some better approach?

Among other things: Does C standard (and de-facto compilers) assume that the structs fields lay in memory in the same order as they are defined in source? This probably is a stupid question, but I’d want to be sure…

UPDATE: Some conclusions from the answers and my own findings:

  1. There seems to be no problem with my approach. In particular, C dictates that struct fields will never change order.

  2. One could also (as suggested by an aswer) count from the last persistent field and add its size : (char*)&myst.x2 + sizeof(&myst.x2) - (char*)(&myst) . That would be equivalent, except that it would include not the padding bytes (if present) for the last field. A very small advantage – and a very small disadvantage, in being less simple.

  3. But the accepted answer, with offsetof, seems to be preferable than my proposal. It’s clear-expressive and pure compile-time, it does not require an instance of the struct. It further seems to be standard, available in any compiler.
    If one does not need a compile-time construct, and has an instance of the struct available (as is my scenario) both solutions are esentially equivalent.

  • 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-19T16:52:30+00:00Added an answer on May 19, 2026 at 4:52 pm

    Have you looked at the offsetof facility? It returns the offset of a member from the start of a struct. So offsetof (st, x2) returns the offset of x2 from the start of the struct. So in your example offsetof (st, x2) + sizeof(st.x2) will give you the count of bytes of the serialized components.

    This is pretty similar to what you are doing now, you just get to ignore the padding after x2 and to use a rarely used piece of C.

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

Sidebar

Related Questions

i want to grab text from HTML do some process and change to it
I want to grab my customers phone number from a MYSQL database and auto
I want to grab the most recent entry from a table. If I was
I want to grab from a db a record with name='John Doe'. I'd like
I want to grab records from a table based on the day of the
I'm grabbing data from a published google spreadsheet, and all I want is the
I have a multi-part array that I want to grab a piece of and
I want to grab a particular column value a.id and store it into a
I am doing an application where I want to grab the content of div,which
I have a flash applet that I want to grab a remote XML file

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.