Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site mit-athena.UUCP Path: utzoo!watmath!clyde!bonnie!akgua!mcnc!decvax!mit-athena!jc From: jc@mit-athena.UUCP (John Chambers) Newsgroups: net.lang.c Subject: Re: Un-alignment in structures Message-ID: <135@mit-athena.UUCP> Date: Fri, 29-Mar-85 09:43:21 EST Article-I.D.: mit-athe.135 Posted: Fri Mar 29 09:43:21 1985 Date-Received: Mon, 1-Apr-85 23:47:40 EST References: <230@tellab2.UUCP> Organization: MIT Project Athena Lines: 73 > In article <120@mit-athena.UUCP> jc@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 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.