Thread: structure 32bit alignment

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    structure 32bit alignment

    Hello everybody, I'm looking how it's possible to make a structure to be 32bit aligned. For example, I need that the 32bit fields in the next structure are 4byte-aligned (so that their address mod 4 == 0 ):
    Code:
    typedef struct{
      UInt16 a;
      UInt16 b;
      UInt32 c;
      ...
    }MyStruct;
    
    MyStruct *pStruct;
    What can I do to be shure that pStruct%4 == 0?

    Thank you very much,
    BrownB

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Use dummy members?
    Code:
    typedef struct{
      UInt16 a;
      UInt16 Dummy1;
      UInt32 b;
      UInt16 c;
      UInt16 Dummy2;
      UInt32 d;
    }MyStruct;
    Not sure if there is a more clever way...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    I'm not shure this will work.. my platform uses only 16 bit alignment: the 16bit values are stored on addresses so that addr % 2 == 0. Now, if my compiler places pStruct so that pStruct % 4 == 0, it's true that:

    Code:
    &( pStruct->c ) % 4 == 0
    -----+-----
    addr | var
    -----+-----
    0008 |   a        //(16b)
    0010 |   dummy1   //(16b)
    0012 |   b        //(32b)
    0016 |   dummy2   //(16b)
    0018 |   c         //(16b)
    ...
    and so your way it's ok. But if my compiler places pStruct so that pStruct % 4 !=0 and pStruct % 2 == 0?
    Code:
    &( pStruct->c ) % 4 == 2 
    -----+-----
    addr | var
    -----+-----
    0010 |   a        //(16b)
    0012 |   dummy1   //(16b)
    0014 |   b        //(32b)
    0018 |   dummy2   //(16b)
    0020 |   c         //(16b)
    ...
    As you see, b is a 32bit field but is 16 bit aligned.
    Cristian

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Using gcc (so this isn't portable), you could use:

    Code:
    typedef struct {
      UInt16 a;
      UInt16 b;
      UInt32 c;
      . . .
    } __attribute__ ((packed)) MyStruct;
    so that there is no padding in struct. And you could also use

    Code:
    typedef struct {
      UInt32 a:16;
      UInt32 b:16;
      Uint32 c;
      . . .
    } __attribute__ ((packed)) MyStruct;
    but "UIntXX" doesn't seem gcc's idiom...

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    This seems a better way, thank you!

    typedefs as UIntXX are PalmOS specific, I think. I'm using gcc, so I think I'll not have any problem.
    Bye!
    BrownB

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > What can I do to be shure that pStruct%4 == 0?
    This doesn't have much to do with the structure.

    If you have a pointer, then you need to make it point somewhere

    1. MyStruct *pStruct = malloc ( sizeof *pStruct );
    malloc always returns memory which is aligned to the worst-case alignment restruction for any type which you can create.
    2. MyStruct myvar; MyStruct *pStruct = &myvar;
    The compiler will always allocate myvar on whatever alignment boundary is required by the structure.

    So if your compiler allocates it on a 2-byte alignment, why do you need it on a 4-byte alignment?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Standard C only allows for bitfields using "int" "signed int" "unsigned int", and C99 allows type _Bool. So you may need to check your implementation. You can also use nameless bitfield elements, and also specify ":0" to force padding.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    I'm sorry I'm not clear enough: my fault...I'll try to be more explicit.

    I need to have a structure where the 32 bit fields are always at addresses which are multiple of 4. Is this possible?

    I think it's the same as ask if is possible to set the address of the first member of a structure to be a multiple of 4, using dummies to align the subsequent fields.

    I don't know if this is simple to do, does someone know?
    Thank you again,
    BrownB

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I still don't see why you need this. Are you trying to overlay a structure on top of something read from a device / file / network?

    Just to correct rmps's post, you need alignment, not pack
    Code:
    struct S { short f[3]; } __attribute__ ((aligned (8)));
    You can fiddle with all sorts of things using gcc attributes.

    It's just that you seem to be going to extreme lengths to solve an alignment problem. Who knows, later on you may have an endian-ess problem..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM