Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site peora.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!mtuxo!mtune!akguc!codas!peora!jer From: jer@peora.UUCP (J. Eric Roskos) Newsgroups: net.unix,net.unix-wizards,net.arch,net.lang.c Subject: Re: get size of malloc'd object Message-ID: <2208@peora.UUCP> Date: Wed, 18-Jun-86 15:19:48 EDT Article-I.D.: peora.2208 Posted: Wed Jun 18 15:19:48 1986 Date-Received: Fri, 20-Jun-86 02:00:31 EDT References: <165@daisy.UUCP> <334@valid.UUCP> <2206@peora.UUCP> Organization: Concurrent Computer Corporation, Orlando, Fl Lines: 24 Xref: watmath net.unix:8233 net.unix-wizards:18467 net.arch:3494 net.lang.c:9457 > Sure it is. Just write your own routine to call malloc; 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. The size of the object is then > found in the word preceeding the location pointed to by the object pointer. hansen@pegasus points out that for this to work, the distance you advance the pointer by has to be sufficient to put the pointer on the boundary malloc usually aligns on. So you have to allocate sizeof(ALIGN) extra space for it to work for all possible types of data, where ALIGN is the largest alignment boundary required for the machine. E.g., on a typical machine it might work for chars and ints, but not double-precision floating point numbers. -- E. Roskos "Hold on, Figment." "Why?" "The Idea Bag is full." "Then, let's imagine--" "--No, wait." -- Dialogue from "Journey Into Imagination," Kodak exhibit, Epcot Center, Orlando, FL.