-
August 18th, 2011, 08:19 AM
#1
Copy buffer to struct
Hi,
i'm receiving some data from an UDP socket, that i would like to copy to a struct/class.
I have defined the following class:
class header
{
public:
unsigned char code;
unsigned char id;
unsigned char length[2];
unsigned char auth[16];
};
I've tried two ways:
First to use memcpy:
header *hdr;
memset(&hdr, 0, sizeof(header));
This works but is not C++.
Second using the following:
header *hdr;
hdr = (header *)data;
Is there a better way to do this?
Or is one of the above the right way?
Thanks in advance
/Torben
-
August 18th, 2011, 08:25 AM
#2
Re: Copy buffer to struct
The first way is definitely wrong as hdr is an uninitialized pointer.
struct members aren't necessarily contiguous memory so your best bet would be to copy the appropriate byes from data to each struct or class member individually.
-
August 18th, 2011, 11:22 PM
#3
Re: Copy buffer to struct
first align your structure you struct boundry so there is no padding introduced by the compiler ,
in VC or GCC you do this by
Code:
#pragma pack(push,1) // this will make sure your struct is on a 1 byte boundary.
// struct deceleration goes here
#pragma pop();
then allocate memory either by malloc or new for you structure. them memcpy
don't forget to cast the struch as (void *) in memcpy see the signature of memcpy.
-
August 19th, 2011, 01:29 AM
#4
Re: Copy buffer to struct
Thanks for the replies )
GCDEF:
I think the first method is the best and fastest way to do this in standard C.
And Yes, you'll have to initialize the pointer.
But it's standard C.
I was seeking the best and fastest way to do this in C++.
I've tried with the string class, and i works fine, and you'll not have to worry about overflow etc.
But i think it's slow or ??
aamir121a:
As mentioned above, memcpy is standard C - and i've read that this is not the best way to do this in C++ - std::copy should have replaced memcpy, but i can't figure it out.
But maybe memcpy is the best way anyway.
I tried the #pragma, but my compiler say "Warning: ignoring #pragma pop".
I'm using CodeBlocks with GCC.
/Torben
-
August 19th, 2011, 07:27 AM
#5
Re: Copy buffer to struct
Originally Posted by tbg
Thanks for the replies )
GCDEF:
I think the first method is the best and fastest way to do this in standard C.
And Yes, you'll have to initialize the pointer.
But it's standard C.
I was seeking the best and fastest way to do this in C++.
I've tried with the string class, and i works fine, and you'll not have to worry about overflow etc.
But i think it's slow or ??
aamir121a:
As mentioned above, memcpy is standard C - and i've read that this is not the best way to do this in C++ - std::copy should have replaced memcpy, but i can't figure it out.
But maybe memcpy is the best way anyway.
I tried the #pragma, but my compiler say "Warning: ignoring #pragma pop".
I'm using CodeBlocks with GCC.
/Torben
Nothing in your first example will copy data to your struct.
-
August 19th, 2011, 08:00 AM
#6
Re: Copy buffer to struct
Originally Posted by tbg
I tried the #pragma, but my compiler say "Warning: ignoring #pragma pop".
IIRC it correctly is #pragma pack(pop) and without the semicolon at the end.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
August 19th, 2011, 08:14 AM
#7
Re: Copy buffer to struct
Originally Posted by GCDEF
Nothing in your first example will copy data to your struct.
You are correct - I missed entering this in the above example: memcpy(&hdr, buf, sizeof(header));
Where buf is the data.
-
August 19th, 2011, 08:17 AM
#8
Re: Copy buffer to struct
Originally Posted by Eri523
IIRC it correctly is #pragma pack(pop) and without the semicolon at the end.
Arhh - got it - thanks
-
August 20th, 2011, 12:03 AM
#9
Re: Copy buffer to struct
Originally Posted by tbg
As mentioned above, memcpy is standard C - and i've read that this is not the best way to do this in C++
C is perfectly valid C++, especially when it makes sense. Nothing is going to be faster at copying a chunk of memory than memcpy.
-
August 20th, 2011, 07:18 AM
#10
Re: Copy buffer to struct
Originally Posted by Chris_F
Nothing is going to be faster at copying a chunk of memory than memcpy.
This is generalization, and, like most generalizations, it just is NOT true.
Yes, memcpy is highly optimized function. It will use SSE if the chunk is large enough. It will also use multi-byte assignment after ensuring proper alignment. But it can't read your mind!
If you KNOW that you only need to copy 20 bytes and can safely assume that everything is at least 4-byte aligned, NOTHING will beat five DWORD copies.
Oh, and don't forget my favorite: the fastest way to do anything is to NOT do it.
Like OP suggested in his first post - simply point you structure to the input buffer. Provided, of course, that their lifetime is the same (or the buffer wil outlive that structure).
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 20th, 2011, 08:44 AM
#11
Re: Copy buffer to struct
Originally Posted by Chris_F
C is perfectly valid C++, especially when it makes sense. Nothing is going to be faster at copying a chunk of memory than memcpy.
Why is it faster ?
If you don't want people to spend your hardwork memory, you also shouldn't have said so. I think
Last edited by Maejie; August 20th, 2011 at 08:49 AM.
-
August 20th, 2011, 08:23 PM
#12
Re: Copy buffer to struct
Originally Posted by Maejie
If you don't want people to spend your hardwork memory, you also shouldn't have said so. I think
What???
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|