packed structs

This is a discussion on packed structs within the C Programming forums, part of the General Programming Boards category; i'm reading into some structs from a file, so of course i need the structs aligned a certain way to ...

  1. #1
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    packed structs

    i'm reading into some structs from a file, so of course i need the structs aligned a certain way to guarantee that everything is read correctly. since i have gcc, i'm using __attribute__ ((packed)). however, all my structs work fine (afaik) except for this one, without the __attribute__ ((packed)), because i guess they are well aligned structs. but why does this one need __attribute__ ((packed)) to work well? or would you have to see more code to know?

    PHP Code:
    // behaves differently (does not load data from file as expected)
    // without __attribute__ ((packed))
    typedef struct
    {
      
    short xoffyoff;
      
    short patchnum;
      
    short onezero;
    __attribute__ ((packed)) patchentry_t
    hello, internet!

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    btw, in the file, the shorts come in the order i just wrote them: xoff, yoff, patchnum, one, and zero.
    hello, internet!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > but why does this one need __attribute__ ((packed)) to work well?
    No idea, because when I compile with DJGPP (also gcc), I get a size of 10 whether it is packed or not (packed makes no difference). This is what I would expect of a structure where all members have the same size and alignment.

    I suggest you first print the sizeof() of this struct when it is packed and unpacked.

    > however, all my structs work fine (afaik) except for this one
    Perhaps this one is being broken by a previous struct (which should be packed) actually reading too much data.

    I suggest you do something like this for each of your structures to make sure they are as you expect
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <stddef.h>
    
    typedef struct
    {
      short xoff, yoff;
      short patchnum;
      short one, zero;
    } /*__attribute__ ((packed))*/ patchentry_t;
    
    int main(void) {
        printf( "%d\n", sizeof(patchentry_t) );
        printf( "%d\n", offsetof(patchentry_t,xoff) );
        printf( "%d\n", offsetof(patchentry_t,yoff) );
        printf( "%d\n", offsetof(patchentry_t,patchnum) );
        printf( "%d\n", offsetof(patchentry_t,one) );
        printf( "%d\n", offsetof(patchentry_t,zero) );
        return 0;
    }

  4. #4
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    thanks, i didn't know the offsetof() thingy. i'll try it out. the sizeof() is right, but without packed the numbers appear to be scrambled. i'm not sure; it probably is other problems, i'll look into it.
    hello, internet!

  5. #5
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    In the first place, 'packing' only takes the padding bytes out of the structure. This is necessary in some cases (pascal is notorious for packed structures).

    Code:
    typedef struct
       {
       short xoff;
       short yoff;
       short patchnum;
       short one;
       short zero;
       }patchentry_t;
    This will use 10 bytes in most cases irregardless of packed or not because the fields already aligns on 16-bit word boundaries for all fields.

    Most compilers align on 16-bit boundaries, unless specificly told to align on long word boundaries. It wastes too many bytes to align on long-word, unless it's necessary for performance.

    What would be _really useful_ would be for you to post one of the structures out of the file-- just the raw hex bytes, for the first 20 bytes or so, and tell us what the fields are supposed to be. Then we could see how the structure in the field is aligned.

    ---

    Normally, you don't need packing. If you have a structure you want saved to disk, simply use that structure as your buffer (pass its address and size to fwrite()). Then, you read it back in the same way, with fread()-- nothing is changed.
    It is not the spoon that bends, it is you who bends around the spoon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 09:30 AM
  2. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 03:46 AM
  3. Passing structs as parameters
    By 182 in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2006, 10:51 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 06:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 01:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21