Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!lll-crg!seismo!ut-sally!utah-cs!utah-gr!thomas From: thomas@utah-gr.UUCP (Spencer W. Thomas) Newsgroups: net.unix,net.unix-wizards,net.arch,net.lang.c Subject: Re: get size of malloc'd object Message-ID: <1742@utah-gr.UUCP> Date: Fri, 13-Jun-86 09:21:33 EDT Article-I.D.: utah-gr.1742 Posted: Fri Jun 13 09:21:33 1986 Date-Received: Tue, 17-Jun-86 07:47:15 EDT References: <165@daisy.UUCP> Reply-To: thomas@utah-gr.UUCP (Spencer W. Thomas) Distribution: net Organization: University of Utah CS Dept Lines: 21 Xref: watmath net.unix:8147 net.unix-wizards:18397 net.arch:3436 net.lang.c:9416 In article <165@daisy.UUCP> misha@daisy.UUCP (Mike Umansky) writes: > I have a need in my application to find the size of a > previously malloc'd object (maybe an array or structure). We had this same need, and for many years satisfied it by writing a macro SIZE(ptr) that `knew' how malloc saved the block size (usually in the word preceding ptr). Finally, with the 4.3 release, and in the interests of easier portability (we were distributing our own malloc with our code), we went to a different scheme: We now do storage allocation via two routines `new' and `dispose'. The function `new' asks malloc for the size requested plus one word, puts the size in the extra word, and returns a pointer to the next word (obviously, it has to worry about alignment, too, so it may not be quite this simple). The function `dispose' readjusts the pointer passed to it and calls free. A third routine, `size', can then easily look at the word preceding the pointer passed to it and return the size of the block. This scheme has the advantage of being blissfully independent of the underlying storage allocation management. -- =Spencer ({ihnp4,decvax}!utah-cs!thomas, thomas@utah-cs.ARPA)