Thread: Size of structures is messed up

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Size of structures is messed up

    Code:
    struct little{
        unsigned char ident;
        unsigned short int data;
    };
    Code:
        char debug[256];
        sprintf(debug,"%i+%i=%i",sizeof(unsigned char),sizeof(unsigned short int),sizeof(little));
        MessageBox(0,debug,"Debug",MB_OK);
    The messagebox says 1+2=4. How is this possible?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Structures have internal and trailing padding to preserve alignment contraints.
    This has been discussed many times before.
    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.

  3. #3
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    How can I get around this "padding"? I want to save it into a binary file as data, but it adds some bytes with random values.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #4

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I just used good old char array. No padding problems.
    So that instead of:
    Code:
    boke[count].ident=i+1;
    boke[count].data=number;
    I have:
    Code:
    boke[count*3]=i+1;
    boke[count*3+1]=number/256;
    boke[count*3+2]=number%256;
    Low-level stuff always helps out.
    Last edited by maxorator; 01-02-2007 at 06:31 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    try to reorder from longest types to shortest...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but it adds some bytes with random values.
    So long as you use the same structure for writing and reading, the content of the padding bytes doesn't matter.

    If you're paranoid about it, memset the whole structure to 0 before assigning individual fields.

    Or use a non-portable "#pragma" of some sort to force the compiler to squeeze out the empty space (at the expense of more complicated code)
    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.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I suppose this should work too:
    Code:
    WORD number=61001;
    char* boke=new char[5*MAXPARTS];
    WORD* arr;
    boke[count*3]=i+1;
    arr=(WORD*)((int)boke+count*3+1);
    *arr=number;
    //do something with it
    delete[] boke;
    Same with integer:
    Code:
    int number=548841;
    char* boke=new char[5*MAXPARTS];
    int* arr;
    boke[count*5]=i+1;
    arr=(WORD*)((int)boke+count*5+1);
    *arr=number;
    //do something with it
    delete[] boke;
    Last edited by maxorator; 01-02-2007 at 07:01 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > int* arr;
    > boke[count*5]=i+1;
    > arr=boke+count*5+1;
    > *arr=number;
    Nope - that will get you a bus error (or unaligned access error) on some machines.
    Did you get any warnings when you compiled it?

    If you must pack ints into a char array, then you need to use memcpy() to be sure of it working.
    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.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    No warnings - work excellenty (I changed the code a bit so it won't give me "couldn't convert" errors)
    I've done exactly the same thing in assembly (where it is normal) before.
    I think this is a very logical thing and I see no problem why it shouldn't work (integers don't always have to start at even memory addresses so I see no reason to get unaligned access error).
    Last edited by maxorator; 01-02-2007 at 07:07 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Good solid "works for me" logic - keep it up
    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.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    It seems as logical as 2+2=4 to me that it works.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So the gcc option of -Wcast-align is basically a waste of time in your opinion then, based on your limited experience?
    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.

  14. #14
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Salem
    So the gcc option of -Wcast-align is basically a waste of time in your opinion then, based on your limited experience?
    I don't think that aligns memory... but I might be wrong.
    Last edited by maxorator; 01-03-2007 at 06:10 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of array
    By goran00 in forum C Programming
    Replies: 38
    Last Post: 04-02-2008, 09:57 AM
  2. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  3. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  4. File Size and File Size on Disk
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-15-2001, 08:03 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM