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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:41:47+00:00 2026-05-29T22:41:47+00:00

some more networking problems. I stumbled into this question: Serialization/Deserialization of a struct to

  • 0

some more networking problems. I stumbled into this question: Serialization/Deserialization of a struct to a char* in C and a lot of the answers are making sense, but what I keep seeing over and over again is this ‘char data’ field that all packets seem to carry.

Now i know what it is for – you store the data you want to send in here. But how does one actually write data to it? Is there ways to just store entire objects in this data field? Or do i somehow serialize the object, store it in data, and then serialize the packet before i send everything off..?

  • 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-29T22:41:48+00:00Added an answer on May 29, 2026 at 10:41 pm

    Every variable exists in your computer’s memory. The memory is organized in bytes.

    When you’re writing C++ code, you can directly read those bytes. For a struct, the memory for all of its members is in one contiguous chunk (although there may be gaps between each member).

    So, if I declare:

    struct foo {
        char x;
        char y;
        short z;
        int q;
    };
    

    Then when I create a struct foo, I get the following layout in memory (8 total bytes on most systems):

     xyzzqqqq
    

    The first byte is x, the second y, the third and fourth together are z, and the last four are q.

    So, the object is already “serialized” – you have a bunch of bytes which represent it. That’s all you need to send over the network: the information which represents the data structure.

    The reason you’d write your own serializer is because you might want to change the way that the object is read or written (for instance, what if I added a field to struct foo?), because you need to communicate between machines where the memory layout is different (which byte of z represents the “most significant” portion of the number?), or because you only want to serialize part of the structure (what if we had some empty space between the members?).

    But, fundamentally, the reason you’re sending “char data” is because everything in your computer can be represented that way. I’m not going into Turing’s proofs about symbol encoding, but it’s a mathematical certitude that any piece of knowledge can be encoded as a series of ones and zeroes.

    In more concrete terms, the way you put data into the “char data” field of a packet is by memcpying from where the data currently are into the buffer. So if I had a char* target, I could write a struct foo x into it thusly:

    memcpy(target, &x, sizeof(struct foo));
    

    Or I could do it more carefully by writing each field:

    memcpy(target, &x.x, 1);
    memcpy(target+1, &x.y, 1);
    memcpy(target+2, &x.z, sizeof(short));
    memcpy(target+4, &x.q, sizeof(int));
    

    The & is the address-of operator, if you didn’t already know. So I’m writing from the address of each member, into some offset within target, and writing a number of bytes equal to the length of the member variable representation.

    The accepted answer to your last question pointed out that this is an oversimplification: when you send a multibyte integer over the network, you have to worry about the endianness (byte order). So what you actually do is this:

    memcpy(target, &x.x, 1);
    memcpy(target+1, &x.y, 1);
    *((short*)(target+2)) = htons(x.z);
    *((int*)(target+4)) = htonl(x.q);
    

    This will handle reversing the bytes as appropriate to convert from host byte order to network byte order. Obviously the one-byte-long values are immune.

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

Sidebar

Related Questions

You may notice from my last question that a problem caused some more problems,
Update: After some more reading I see that this problem is totally general, you
back again with some more SQLAlchemy shenanigans. Let me step through this. My table
Maybe I am just over thinking this and need to write some more prototype
If I have some xml like this: <mynode> <mysubnode> <mysubsubnode>hello world</mysubsubnode> some more text
You're probably a lot to be subscribers to various mailing list, some more updated
i just got some more questions while learning PHP, does php implement any built
I have several strings in the rough form: [some text] [some number] [some more
My data looks as follows MyText ------- some text, some more text, even more
My data looks like ID MyText 1 some text; some more text 2 text

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.