Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Serialize / De-Serialize struct to Buffer

Serialize / De-Serialize struct to Buffer

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestionannouncementlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _NielsB
    wrote on last edited by
    #1

    Well, another beginner issue from me. I want to serialize several structs to a char array. In the end, the char* should point to smth like this: Finnaly, i want to de-serialize the B2 from this array to a new B2_dupe; The problem: with my code, B2_dupe is not filled correctly. If you dont mind, please have a look at the following code- #pragma pack(1) struct A { A(){ Sig1 = 'A'; Sig2 = 'B'; Sig3 = 'C'; Version = '1'; Numbers = 2; Reserved = 0; Offset = 0; } char Sig1; char Sig2; char Sig3; char Version; char Numbers:7; char Reserved:1; unsigned long Offset; }; struct B { B(){ Id=0; Reserved=0; Offset=0;} char Id:7; char Reserved:1; long Offset; }; #pragma pack() // calc size int sizeA = sizeof A; int sizeB = sizeof B; // struct A A a; a.Sig1 = 'D'; // struct B #1 B b1; b1.Id = 0; b1.Offset = 0; // struct B #2 B b2; b2.Id = 1; b2.Offset = 0; // serialize int nSizeSer = (2*sizeB)+sizeA; char* pBuff = new char[nSizeSer]; memcpy(pBuff, &a, sizeA); memcpy(pBuff+sizeA, &b1, sizeB); memcpy(pBuff+sizeB, &b2, sizeB); // de-serialize B b2_dupe; int offset = sizeA+sizeB; pBuff+=offset; //move ptr memcpy(&b2_dupe,pBuff,sizeB); I checked b2_dupe.Offset and this should be zero ... but it isnt (-842150451). Why is that? Thanks in advance :)

    C 1 Reply Last reply
    0
    • _ _NielsB

      Well, another beginner issue from me. I want to serialize several structs to a char array. In the end, the char* should point to smth like this: Finnaly, i want to de-serialize the B2 from this array to a new B2_dupe; The problem: with my code, B2_dupe is not filled correctly. If you dont mind, please have a look at the following code- #pragma pack(1) struct A { A(){ Sig1 = 'A'; Sig2 = 'B'; Sig3 = 'C'; Version = '1'; Numbers = 2; Reserved = 0; Offset = 0; } char Sig1; char Sig2; char Sig3; char Version; char Numbers:7; char Reserved:1; unsigned long Offset; }; struct B { B(){ Id=0; Reserved=0; Offset=0;} char Id:7; char Reserved:1; long Offset; }; #pragma pack() // calc size int sizeA = sizeof A; int sizeB = sizeof B; // struct A A a; a.Sig1 = 'D'; // struct B #1 B b1; b1.Id = 0; b1.Offset = 0; // struct B #2 B b2; b2.Id = 1; b2.Offset = 0; // serialize int nSizeSer = (2*sizeB)+sizeA; char* pBuff = new char[nSizeSer]; memcpy(pBuff, &a, sizeA); memcpy(pBuff+sizeA, &b1, sizeB); memcpy(pBuff+sizeB, &b2, sizeB); // de-serialize B b2_dupe; int offset = sizeA+sizeB; pBuff+=offset; //move ptr memcpy(&b2_dupe,pBuff,sizeB); I checked b2_dupe.Offset and this should be zero ... but it isnt (-842150451). Why is that? Thanks in advance :)

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      The line

      _NielsB wrote:

      memcpy(pBuff+sizeB, &b2, sizeB);

      should be:

      memcpy(pBuff+sizeA+sizeB, &b2, sizeB);

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      [my articles]

      _ 1 Reply Last reply
      0
      • C CPallini

        The line

        _NielsB wrote:

        memcpy(pBuff+sizeB, &b2, sizeB);

        should be:

        memcpy(pBuff+sizeA+sizeB, &b2, sizeB);

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        [my articles]

        _ Offline
        _ Offline
        _NielsB
        wrote on last edited by
        #3

        Ah ... thank you again :)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups