Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site ucsfcgl.ucsfcgl.UUCP Path: utzoo!watmath!clyde!burl!ulysses!ucbvax!ucsfcgl!arnold From: arnold@ucsfcgl.UUCP (Ken Arnold%CGL) Newsgroups: net.unix,net.unix-wizards,net.arch,net.lang.c Subject: Re: get size of malloc'd object Message-ID: <9894@ucsfcgl.ucsfcgl.UUCP> Date: Tue, 24-Jun-86 19:54:58 EDT Article-I.D.: ucsfcgl.9894 Posted: Tue Jun 24 19:54:58 1986 Date-Received: Sat, 28-Jun-86 03:20:17 EDT References: <165@daisy.UUCP> <334@valid.UUCP> <2206@peora.UUCP> <2081@umcp-cs.UUCP> Reply-To: arnold@ucsfcgl.UUCP (Ken Arnold%CGL) Organization: Computer Graphics Laboratory, UCSF Lines: 23 Xref: watmath net.unix:8411 net.unix-wizards:18613 net.arch:3588 net.lang.c:9597 In article <2081@umcp-cs.UUCP> chris@umcp-cs.UUCP (Chris Torek) writes: >In article <2206@peora.UUCP>, jer@peora.UUCP (J. Eric Roskos) writes: >>... allocate a sizeof(int) worth of extra space, then store the >>size of the thing you malloc'ed in the int at the front of the >>allocated block, advance the pointer past the place where your >>stored the size, and return that as the pointer to the block you >>allocated. >> > >Unfortunately, it is not necessarily portable. I can imagine a >machine where `double' arguments must be aligned on an eight-byte >boundary, but integers are only four bytes wide. In this case >calling the new routine to allocate doubles would result in an >odd-address-style trap when the returned value is used. So put the int at the end of the space, after increasing the size to be allocated by sizeof (int) + enough space to align the int on a byte which is a multiple of sizeof (int). Then return the original malloc()ed pointer. Can we say "hack"? Ken Arnold