unaligned access on Sparc Solaris
Joe Buck
jbuck@racerx.synopsys.com
Thu Oct 12 15:11:00 GMT 2000
>
>
> On gcc on Sparc Solaris I need to be able to access data on unaligned
> boundries...
> For instance:
>
> char buffer[256];
> int *i=(int *)(buffer+1);
> (*i)=3;
>
> Right now the 3rd line causes a bus error.... Is there a compiler flag
> or pragma that will allow me to do this?
No.
> Sun's compiler has a -misalign
> flag, but I'm trying to get away from it because it's too buggy...
And you wouldn't want to use it even if it worked reliably, because it
slows down *every* access to memory, not just the few that need to be
unaligned. The physical hardware of the Sparc cannot read and write
unaligned data! To implement it, the compiler has to produce lots of
read-byte, write-byte and shift operations.
Even for processors that support unaligned words, unaligned access is much
slower.
Suggestion: implement the functions
int read_unaligned_int(const char* address);
void write_unaligned_int(char* address, int value);
by using unions, e.g.
union int_or_char {
int asInt;
char asChar[sizeof(int)];
};
Now you can write
char buffer[256];
write_unaligned_int(buffer+1, 3);
and the person who comes along after you can maintain your code.
More information about the Gcc
mailing list