More info I received from Wildrice forum
Peter Barada wrote:
>>I tested it by checking its size , behavior , etc..and it is OK ...
>>Now I can create any kind of structure...it's very
>>confortable...specially if you have already applications made based on
>>structures ..
>
>Remember that you're making a trade here. Since the "#pragma packed"
>tells the compiler to ignore the alignment of bothe the structuer, and
>its members, then any access to a member requires that it is loaded
>into registers using bytes, so if you have an int, the compiler will
>emit four byte loads as well as three shifts to load the value. The
>same for a store. This can dramatically increase both the size of the
>code, as well as the execution time.
>
>As an example, gcc-3.4.3 compiles the following code for -m5307:
>
>struct foo {
> int x;
> char y;
>} __attribute__ ((packed));
>
>int bar(struct foo *p)
>{
> return p->x;
>}
>
>Into (removing the fuction prologue/epilogue code):
>
>move.b (%a0),%d1
>moveq #24,%d0
>lsl.l %d0,%d1
>move.b 1(%a0),%d0
>swap %d0
>clr.w %d0
>and.l #16711680,%d0
>or.l %d1,%d0
>move.b 2(%a0),%d1
>lsl.l #8,%d1
>and.l #65280,%d1
>or.l %d0,%d1
>clr.l %d0
>move.b 3(%a0),%d0
>or.l %d1,%d0
>
>So to access member 'x' from the 'foo' structure whose address is in %a0,
>leaving it in %d0 while using %d1 as a temporary takes 15 instructions
>consuming 48 bytes instead of one instruction that consumes 2 bytes,
>or 15 times as slow, and 24 times as big.
>
>Just some food for thought.
>
While that is something you don't want normally, it's very nice that gcc
will make the correct code when you *do* need it. Occasionally you meet
a structure that is defined externally and doesn't fit the proper
alignments, making the "packed" attribute very handy. If only there
also was a "little-endian" attribute (Diab Data's compiler has that
feature, IIRC).
and ...
You are right. But sometimes you have to live with it. For example, accessing SCSI structures or FAT12/FAT16/FAT32 structures from your application requires the struct fields not to be aligned.
True, but one possiblity is to extract from the unaligned struct theinformation you need just once, and the refer to that information viaaligned variables. This can reduce both execution time *and* codespace...
For small pieces of code, yes. But try to write a full file system this way...