Hi,
What I ment was, if you use the following code:
struct unaligned {
char first;
int second;
} __attribute__((packed));
union test {
struct unaligned data;
char cdata[3];
};
void function() {
union test test;
int data, tmp;
test.cdata[0] = 0x1;
test.cdata[1] = 0x2;
test.cdata[2] = 0x3;
data = test.data.second;
memcpy(&tmp, &test.data.second, 2);
}
data will contain the value 0x201, and not the value
0x302 as it should. The same goes for tmp (try it. I
did and got the wrong results). The reason the memcpy
goes wrong is because it is replaced by a single word
move instead of two byte moves.
When I built gcc with the STRICT_ALIGNMENT macro set
to 1, I got the value 0x302 which is correct.
G. Halkes
PS: I used a printf routine to output the results to a
serial port to see the result. I removed it from the
code because it isn't standard. Just in case your
wondering.
>for built-ins gcc takes into account alignement.
> So, if gcc knows, that something aligned by 2 it
uses >world operations,
> otherwise byte ones.
>
> Packed does not mean the structure misaligned.
> It depends on data types of the structure.
> For example:
>
> struct {
> char a;
> int b;
> };
>
> isn't aligned by world boundary.
>
> struct { int a,b}
> aligned.
> Even more -- if the structure aligned and its size
is >not even, gcc will use
> world operations for thhis structure size()-1 and
>byte operation for a last
> byte of it.
__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/