Suitably aligned pointers

The Beach Bum jfh at rpp386.Dallas.TX.US
Sun Oct 16 14:19:25 AEST 1988


In article <345 at marob.MASA.COM> daveh at 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 at rpp386.Dallas.TX.US       |          "Just say `Gno'"
UucpNet : <backbone>!killer!rpp386!jfh  +--------------------------------------



More information about the Comp.lang.c mailing list