Xref: utzoo comp.lang.c:22625 comp.unix.wizards:18583 Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!ulysses!cjc From: cjc@ulysses.homer.nj.att.com (Chris Calabrese) Newsgroups: comp.lang.c,comp.unix.wizards Subject: Re: How do I keep pointers aligned? Summary: all pointers are not created equal some machines have weird architectures Message-ID: <12259@ulysses.homer.nj.att.com> Date: 9 Oct 89 21:13:07 GMT References: <12879@s.ms.uky.edu> Organization: AT&T Bell Laboratories, Murray Hill Lines: 43 In article <12879@s.ms.uky.edu>, sean@ms.uky.edu (Sean Casey) writes: > Lint tells me I have possible pointer alignment problems. > > In one case, I'm allocating storage for a struct: > > typedef struct Stuff { > int stufflet; > etc; > } STUFF; > > s = (STUFF *) malloc((unsigned) sizeof(STUFF)); > > Now we all know malloc returns a (char *). What I'm wondering is why > lint would complain. Aren't all pointers the same size? Does lint think > that perhaps a four byte pointer might be copied to a two byte pointer > that is not aligned so that it can be interpreted as a 4BP? > > [ further examples and comments deleted ] No, all pointers are not created equal! I worked for a while on a machine (no longer in production) which had a custom mmu on a 68010 (this was back before the official Motorola mmu's came out) which did all sorts of weird things with word aligned pointers of different sizes and really made my head spin sometimes (this was back when I was a freshman undergraduate before I learned about lint -p). Come to think of it, you can do the same thing on Intel i[023]86 architectures. Even on normal architecures, however, (char *) must be able to point to every memory address, whereas (STUFF *) may only be able to point to word aligned addresses (for efficiency's sake). Malloc makes sure that it only passes word alighed pointers, but lint doesn't know that. K&R [ANSI] C guarentees that (STUFF *)(char *)type_stuff [(STUFF *)(void *)type_stuff] works, but not the other way around. Hopefully, every malloc implementation gets it right! -- Name: Christopher J. Calabrese Brain loaned to: AT&T Bell Laboratories, Murray Hill, NJ att!ulysses!cjc cjc@ulysses.att.com Obligatory Quote: ``Anyone who would tell you that would also try and sell you the Brooklyn Bridge.''