Thread: memcpy question

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    2

    memcpy question

    I'm seeing a weird problem with memcpy. I have the following code:

    Code:
    int main()
    {
            char data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    
    
            struct mystruct {
    
                    char a;
                    short b;
                    char c;
                    long d;
                    char e;
                    short f;
                    char g;
            } ms;
    
            memcpy(&ms, data, 16);
    Ok, I expected a = 1, but for some bizarre reason, c = 5! It skipped over 4 completely. Not only that, but b = 3 and 4, it skipped over 2! This totally messes up all of my code.

    Whats the best way to fix this issue without manually specifying offsets (i.e. c = data[3]) and make sure all the other values equal what they are supposed to?
    Last edited by absoult-tech1; 10-02-2010 at 11:41 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Google struct padding.
    Even if there's no padding, it won't work.(as what you expect).
    Just assign to individual member.
    Last edited by Bayint Naung; 10-02-2010 at 11:43 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    2
    Quote Originally Posted by Bayint Naung View Post
    Google struct padding.
    Just assign to individual member.

    I know its padding the structure. My question is, how do I solve that problem without manually specifying offsets? It becomes an issue with portability and endianness between machines, and much further complicates the code.
    Last edited by absoult-tech1; 10-02-2010 at 11:45 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Structs may include padding (spare space between members) for performance reasons. memcpy() will happily write data to those padding bytes.

    Rather than trying to use memcpy() to initialise the struct - such things are sensitive to how the compiler lays out the structure in memory - initialise the struct directly.

    Code:
    struct mystruct ms = {1, 1027, 4, <other values you expect>};
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    ms.a = data[0];
    ms.b = (data[1]<<8) + data[2];
    etc etc
    If you want portability across all endian implementations and struct packing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Reorder the variables in your struct so that they are ordered from largest to smallest. (long -> short -> short -> char -> char -> char -> char)
    That minimises the padding.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. quick memcpy question
    By newbie30 in forum C Programming
    Replies: 3
    Last Post: 01-25-2010, 01:01 PM
  2. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 05:52 PM
  3. memcpy question
    By newbie30 in forum C Programming
    Replies: 2
    Last Post: 12-09-2009, 08:37 AM
  4. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM