Allign it or you loose it
i guys i have a struct defined as below
Code:
typedef struct s_myStruct
{
uint8 A;
uint8 B;
uint8 C;
uint8 D;
uint32 E;
uint16 F;
} t_myStruct;
by the way i have it defined somewhere these uint stuff as
Code:
typedef char int8;
typedef unsigned char uint8;
typedef short int int16;
typedef long int int32;
typedef unsigned short int uint16;
typedef unsigned long int uint32;
Now, my question is that when i do a
Code:
sizeof (t_myStruct)
it will come back as 12 , I was expecting 10.
what did i do wrong please guide me ...
baray98
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:
Code:
struct Foo
{
...
} __attribute__((packed));
(of course it won't work with other compilers).
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
Re: Allign it or you loose it
You can use -fpack-struct, but I'm not sure if you really want this.
Quote:
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.