Results 1 to 4 of 4

Thread: Allign it or you loose it

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Allign it or you loose it

    i guys i have a struct defined as below
    Qt Code:
    1. typedef struct s_myStruct
    2. {
    3. uint8 A;
    4. uint8 B;
    5. uint8 C;
    6. uint8 D;
    7. uint32 E;
    8. uint16 F;
    9. } t_myStruct;
    To copy to clipboard, switch view to plain text mode 

    by the way i have it defined somewhere these uint stuff as
    Qt Code:
    1. typedef char int8;
    2. typedef unsigned char uint8;
    3. typedef short int int16;
    4. typedef long int int32;
    5. typedef unsigned short int uint16;
    6. typedef unsigned long int uint32;
    To copy to clipboard, switch view to plain text mode 

    Now, my question is that when i do a
    Qt Code:
    1. sizeof (t_myStruct)
    To copy to clipboard, switch view to plain text mode 
    it will come back as 12 , I was expecting 10.

    what did i do wrong please guide me ...
    baray98

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Allign it or you loose it

    Because this structure was aligned to word (i.e. "double word" on IA-32) boundary. If you want to make it smaller, you have to tell your compiler to pack it.

    In GCC/MinGW you can do this using attributes:
    Qt Code:
    1. struct Foo
    2. {
    3. ...
    4. } __attribute__((packed));
    To copy to clipboard, switch view to plain text mode 
    (of course it won't work with other compilers).
    Last edited by jacek; 27th September 2007 at 13:41. Reason: too many underscores

  3. #3
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Allign it or you loose it

    jacek,

    in my apps i have so many struct that i am expecting it to allign in bytes (8 bit) is there any other option in the compiler so that the whole app will allign in bytes i am using GCC/MingW by the way .

    thanks for the quick reply

    baray98

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Allign it or you loose it

    You can use -fpack-struct, but I'm not sure if you really want this.

    Warning: the -fpack-struct switch causes GCC to generate code that is not binary compatible with code generated without that switch. Additionally, it makes the code suboptimal. Use it to conform to a non-default application binary interface.

  5. The following user says thank you to jacek for this useful post:

    baray98 (28th September 2007)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.