Serialize / De-Serialize struct to Buffer
-
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 :) -
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 :)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] -
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]