Do any of you guys have experience of using misaligned memory accesses for any reason?
Usually, it's something you want to avoid, because it's both a non-portable behaviour, and is slower than an aligned access.
However my current project has compelling reasons to do so. (It's an engine for running Conways "game of life" if you're interested).
For instance this simple fragment:
This takes array foo, and updates bytes 13 - 16 within it.Code:int foo; int word = 3; int offset = 1; int *misaligned = (int*)((char*)&foo[word] +offset); *misaligned += 0x12345678;
I made foo an array of ints here, because it's accessed that way elsewhere. This isn't a totally fictitious example, I actually do something like this in my code.
Is there a neater way to do this? All those casts look untidy!