I just found something weird, and I am trying to figure out if this is
normal or not. I am using GCC to compile a program on the
Beagleboard under Ubuntu 10.04. If I declare the following structure:
Typedef struct ss_struct{
Int a;
Int b;
Double c;
}ss_t;
Typedef struct tt_struct{
Int a;
Double b;
Int c;
}tt_t;
And I compare them…they aren’t the same size.
Sizeof(ss_t) = 16
Sizeof(tt_t) = 24
Is this some kind of alignment thing on the Beagleboard to align to
the largest data type in a structure? It doesn’t do this on x86
running Ubuntu 10.04. I get 16 in both configurations.
Any insight anyone could give me on this would be greatly appreciated.
Thanks,
-Ted
Hi Ted,
Even more, if you try to compile next code you'll see interesting
results as well:
typedef struct
{
char a;
char b;
double c;
long long d;
} ss_struct;
typedef struct
{
char a;
double b;
char c;
long long d;
} tt_struct;
sizeof(ss_struct) = 24
sizeof(tt_struct) = 32
I guess it's because of 8 byte stack alignment requirement for ARM
(not for Beagleboard):
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka4127.html
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0472c/Cacbjbad.html
To avoid this behaviour you can use __attribute__ ((packed)), for
example:
typedef struct
{
char a;
char b;
double c __attribute__ ((packed));
long long d __attribute__ ((packed));
} ss_struct;
typedef struct
{
char a;
double b __attribute__ ((packed));
char c;
long long d __attribute__ ((packed));
} tt_struct;
sizeof(ss_struct) = 18
sizeof(tt_struct) = 18
Cheers,
Max.