Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!chinet!dag From: dag@chinet.UUCP (Daniel A. Glasser) Newsgroups: comp.lang.c Subject: Re: what C needs -- packed structures Message-ID: <2083@chinet.UUCP> Date: 9 Jan 88 02:38:41 GMT References: <8801071842.AA04669@decwrl.dec.com> Reply-To: dag@chinet.UUCP (Daniel A. Glasser) Organization: Chinet - Public Access Unix Lines: 65 In article <8801071842.AA04669@decwrl.dec.com> devine@cookie.dec.com (Bob Devine) writes: [lots of junk deleted] > A different argument for providing packed/unpacked structures is >simply to allow the programmer to choose between access speed or >smaller data segment. As an example of prior art, Microsoft C has >a compile time flag for this. Granted, not all structures would >be packed the same way or even could be packed. Some architectures >allow ints to be only integer aligned (pyramid, I believe) while >others all ints to be char aligned (vax). > > Consider this suggestion in light of the C philosophy or letting >the programmer decide. This is not so easy. Yes, the VAX, 8088, and many other processors allow access of data at any byte alignment, but many don't. If you have a packed type on a PDP-11 or 68000 you must use several instructions to fetch a word or long or other multi-byte type any time you access any structure through a pointer. Take the following bit of C code: struct yerks { char y_foo; int *y_fie; char y_poo; }; struct yerks *w; int i; i = *w->y_fie; The assignment, on a PDP-11, takes code to the effect of the following: mov _w, r0 clr -(sp) movb 1(r0), (sp) movb 2(r0), 1(sp) mov @(sp)+, _i Now, assume that you are on a 68000 -- movea.l _w, a0 moveq $0, d0 move.l d0, -(sp) move.b 1(a0), (sp) move.b 2(a0), 1(sp) move.b 3(a0), 2(sp) move.b 4(a0), 3(sp) movea.l (sp)+, a0 move.w (a0), _i; Okay, that not seem too painful? No, packed structures do not belong in the standard. A particular compiler can pack structures as an option, or by default, but that is not going to be portable. Period. C is supposed to be an efficient language. If you really need packed structures, and want them portable, code them directly with macros and unions. -- Daniel A. Glasser ...!ihnp4!chinet!dag ...!ihnp4!mwc!dag ...!ihnp4!mwc!gorgon!dag One of those things that goes "BUMP!!! (ouch!)" in the night.