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.
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.
Re: Copy buffer to struct
Thanks for the replies :o)
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
Re: Copy buffer to struct
Quote:
Originally Posted by
tbg
Thanks for the replies :o)
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.
Re: Copy buffer to struct
Quote:
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.
Re: Copy buffer to struct
Quote:
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.
Re: Copy buffer to struct
Quote:
Originally Posted by
Eri523
IIRC it correctly is #pragma pack(pop) and without the semicolon at the end.
Arhh - got it - thanks :)
Re: Copy buffer to struct
Quote:
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.
Re: Copy buffer to struct
Quote:
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).
Re: Copy buffer to struct
Quote:
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
Re: Copy buffer to struct
Quote:
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???