Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!cs.utexas.edu!rutgers!bellcore!tness7!texbell!killer!pollux!dalsqnt!rpp386!jfh From: jfh@rpp386.Dallas.TX.US (The Beach Bum) Newsgroups: comp.lang.c Subject: Re: Suitably aligned pointers Message-ID: <7949@rpp386.Dallas.TX.US> Date: 16 Oct 88 04:19:25 GMT References: <345@marob.MASA.COM> Reply-To: jfh@rpp386.Dallas.TX.US (The Beach Bum) Organization: River Parishes Programming, Dallas TX Lines: 33 In article <345@marob.MASA.COM> daveh@marob.UUCP (Dave Hammond) writes: >We use a pretty basic set of memory management routines which >allocate large hunks of memory and parcel it out as requested, >saving malloc the headache of managing huge lists of small buffers. >The problem is the routines don't return a `suitably aligned' pointer, >so the accuracy of casting the return to other than char * is in doubt. > >Can someone explain the technique used to suitably align a pointer ? You can't do it in a portable fashion. Different hardware has different requirements for producing a ``suitably aligned'' pointer. Your best bet is to determine the alignment requirements for the largest hardware supported object, i.e., double precision float. Then, find a way to create a pointer properly aligned for that object. The following is only food for thought. Don't believe a word of it ... One good approach would be to hand out memory in units of some type T, where T is this largest supported object. Memory is now a [ presumably ] linear array of objects of size sizeof(T), and since T requires maximal alignment [ so the theory goes ], allocating a new memory region in the array will automatically produce an address which is maximally aligned. This pointer then needs to be coerced into some form which can be cast into any other pointer type. [ which as everyone will tell you is generally a no-op anyhow ] Hope this helps somewhat. -- John F. Haugh II +----Make believe quote of the week---- VoiceNet: (214) 250-3311 Data: -6272 | Nancy Reagan on Richard Stallman: InterNet: jfh@rpp386.Dallas.TX.US | "Just say `Gno'" UucpNet : !killer!rpp386!jfh +--------------------------------------