468,512 Members | 1,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

serialize a struct

Hi,

I have a structure like this:
struct TEST {
int x, y, z;
string str;
vector<floatvFloat;
};
I need to serialize an instance of this struct into a stream of bytes
(its for insertion into a 3rd party file). This 3rd party library has a

function to insert the serialized buffer into their files that looks
like:
SetPrivateData(unsigned char *pData, int nSize);
It was suggested I do the following:
TEST t;
// how much space do we need?
int bufsize = sizeof(int)* 3 + sizeof(float)*t.vFloat.size() +
t.str.length()+1;
// create and clear the buffer
unsigned char *buf = new unsigned char[bufsize];
memset(buf, 0, bufsize);
unsigned char *pBuf = buf;
int *pIntBuf = (int *) buf;
// copy in the ints
*pIntBuf = t.x;
pIntBuf++;
*pIntBuf = t.y;
pIntBuf++;
*pIntBuf = t.z;
*pIntBuf++;
// copy in the floats
pBuf = (unsigned char *)pIntBuf;
vector<float>::pointer floatptr = &(t.vFloat[0]);
memcpy(pBuf, floatptr, t.vFloat.size()*sizeof(float);
pBuf += t.vFloat.size()*sizeof(float);
// copy in the string
strcpy(pBuf, t.str.c_str());
But I'm not sure if this is working correctly, the above executes but
dies when I try printing any portion of it to screen to examine the
contents using printf(). Is the above doing what I need it to do?
Thanks

Sep 3 '06 #1
1 3230
On 3 Sep 2006 11:15:24 -0700 in comp.lang.c++, "markww"
<ma****@gmail.comwrote,
>But I'm not sure if this is working correctly, the above executes but
dies when I try printing any portion of it to screen to examine the
contents using printf(). Is the above doing what I need it to do?
I find the style of the mentioned code to be a step backward for
someone who is properly using string and vector, but let's not get
into that (unless you want to.) The actual problem I notice is that
you do not write out the vector size before the elements, and so
will have some difficulty figuring out how many elements to read
back later.

As for printf, I find it curious that the thing that is not working
for you is the ONE thing you did not post so that we could look at
it. Instead of printf, try e.g.

std::cerr << std::hex;
std::copy(buf, pbuf, std::ostream_iterator<int>(std::cerr, " "));

Note the <intis significant.

Sep 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Papa.Legba.666 | last post: by
16 posts views Thread by Corne' Cornelius | last post: by
5 posts views Thread by David Sworder | last post: by
5 posts views Thread by objectref | last post: by
reply views Thread by CowPad | last post: by
7 posts views Thread by markww | last post: by
1 post views Thread by markww | last post: by
3 posts views Thread by =?Utf-8?B?Q2VjY28gKElUQSk=?= | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.