CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2011
    Posts
    6

    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    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.

  4. #4
    Join Date
    Feb 2011
    Posts
    6

    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

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Copy buffer to struct

    Quote Originally Posted by tbg View Post
    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.

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Copy buffer to struct

    Quote Originally Posted by tbg View Post
    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.

  7. #7
    Join Date
    Feb 2011
    Posts
    6

    Re: Copy buffer to struct

    Quote Originally Posted by GCDEF View Post
    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.

  8. #8
    Join Date
    Feb 2011
    Posts
    6

    Re: Copy buffer to struct

    Quote Originally Posted by Eri523 View Post
    IIRC it correctly is #pragma pack(pop) and without the semicolon at the end.
    Arhh - got it - thanks

  9. #9
    Join Date
    Aug 2008
    Posts
    902

    Re: Copy buffer to struct

    Quote Originally Posted by tbg View Post
    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.

  10. #10
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Copy buffer to struct

    Quote Originally Posted by Chris_F View Post
    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...

  11. #11
    Join Date
    Jun 2010
    Posts
    115

    Re: Copy buffer to struct

    Quote Originally Posted by Chris_F View Post
    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.

  12. #12
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Copy buffer to struct

    Quote Originally Posted by Maejie View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured