Portablity using structures and malloc - Help

Chris Torek chris at umcp-cs.UUCP
Sun Jul 21 13:31:41 AEST 1985


> malloc knows [the alignment constraints of the machine], but it's
> a pity that you can't make malloc tell.  If there were a nice
>
>   int malign() {return ALIGNMENT_MULTIPLE;}

True.  However, there is a general rule you can use that I've not
yet seen fail on any machine: align your object on an ``n'' bit
boundary, where ``n'' is the smallest power of 2 that is not less
than the size of the object you're allocating.  (Of course this
can be quite wasteful for large areas....)

In case what I wrote doesn't say what I really meant, here's an
example:

	int malign(size)
	register int size;
	{
		register int n = 0;

		while ((1 << n) < size)
			n++;
		return (1 << n);
	}
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at maryland



More information about the Comp.unix mailing list