Un-alignment in structures
John Chambers
jc at mit-athena.UUCP
Sat Mar 30 00:43:21 AEST 1985
> In article <120 at mit-athena.UUCP> jc at mit-athena.UUCP (John Chambers) writes:
> >If the data is unaligned (...) someone has to write the inefficient code to
> >extract the data. It is either me or the compiler. This is not a difficult
> >job for a compiler to do.... Handling
> >misaligned data is an especially dreary piece of drudge work that is both
> >hard for me and easy for the machine.
>
> OK, how does the compiler know at compile time whether a pointer will be
> pointing to an aligned structure or not? It would have to assume that is
> never is aligned, which would produce needlessly innefficient code 95% of
> the time.
Good point, but not really relevant. You see, current C compilers have
this problem right now. Try an experiment like:
#include <stdio.h>
int a[4] = {1,2,3,4};
int i, *p;
main() {
for (i=0; i<9; i++) { /* First 9 "integers" */
p = (int*)((int)a + i); /* Subvert C's casts */
printf("%04x: %4x=%d\n",p,*p,*p);
}
return 0;
}
On our VAX (4.2BSD), the result is:
1030: 1=1
1031: 2000000=33554432
1032: 20000=131072
1033: 200=512
1034: 2=2
1035: 3000000=50331648
1036: 30000=196608
1037: 300=768
1038: 3=3
Even on machines that "can't do" unaligned integers, this program
will probably run without "error" and will give some garbage for
output. The point is that C programs can stuff any value whatsoever
into pointers. After all, what's to stop them? Unless you violate
hardware bounds, you will usually get something. At worst, the
low-order bits will be ignored, and you'll get one of the "adjacent"
aligned words.
So the argument that not doing alignment would require that the
compiler handle funny pointers is a red herring. It could do what
current compilers do--assume that all pointers are valid and generate
code to blindly do the operations.
Anyhow, you changed the subject. It wasn't about valid/invalid
pointers. It was about alignment of fields within structures.
Personally, I'd vote for adding Pascal's "packed" attribute to
C structures. Much as I don't love Pascal, I gotta admit that
this time they did something right. Let's face it, there are
reasons for wanting things aligned, and reasons for wanting them
unaligned. It's yet another space/time tradeoff. Packed structures
waste cpu time to save memory; aligned structures waste memory
to save cpu time. Both are desirable. It's unfortunate that
the guys at Bell didn't get around to adding this goodie to C
before the language became entrenched.
My arguments against automatic alignment come from the fact that
I have been burned by it over and over, while I am not aware of
any case where it has helped me. (But then, you notice the bad
times more than the good.) After all, what does it gain a man
to save a millisecond of his computer's time if he loses an hour
of his own time thereby?
--
John Chambers [...!decvax!mit-athena]
If you're not part of the solution, then you're part of the precipitate.
More information about the Comp.lang.c
mailing list