Alignment - Structures and Other Things

Steffen Dettmer steffen.dettmer@googlemail.com
Thu Apr 14 08:47:00 GMT 2011


On Wed, Apr 13, 2011 at 9:56 PM, kevin diggs <diggskevin38@gmail.com> wrote:
> Even if it doesn't break it will likely incur a performance penalty
> from unaligned accesses, right?

Yes, in best case it will only be slow.
(however, you may have a 8 bit CPU or so, needing byte-boundary
alignment only :)).

I had some ARM boards where things like
  ((struct foo*)(&recv_buf[123]))->mess.cmd
do crash (but code was not generated with gcc), as far as I know
it is possible that compilers generate code to access using
multiple aligned access instructions, but I guess some CPUs do
this internally automatically (because the same binary code did
not crash on different [older/smaller/slower] ARM CPUs).

So I think the code example with (struct f*)(malloc(s)+1) may run
(slowly) in exceptional/lucky cases, but is not correct and
should be fixed, if possible.

oki,

Steffen



More information about the Gcc-help mailing list