Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: net.unix,net.unix-wizards,net.arch,net.lang.c Subject: Re: get size of malloc'd object Message-ID: <1358@brl-smoke.ARPA> Date: Sun, 15-Jun-86 02:11:52 EDT Article-I.D.: brl-smok.1358 Posted: Sun Jun 15 02:11:52 1986 Date-Received: Sat, 21-Jun-86 07:39:22 EDT References: <165@daisy.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Distribution: net Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 21 Xref: watmath net.unix:8253 net.unix-wizards:18490 net.arch:3507 net.lang.c:9493 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). - For example, one routine may allocate some array or just - one big chunk of memory and pass a pointer to the chunk to - another routine. This other routine now needs to know the - size of the area pointed to by this pointer. Also, this - other routine has no idea about the structure of this area - and doesn't even care. Now, the question, how can this - other routine get the size of the area pointed to by passed - pointer. Would (sizeof(*ptr)) work or will it just give - me the size of the pointer type? Another possibility would - be to write a routine which would scan malloc's allocation - array and try to match the pointers; then the size is - easily obtained. Sorry to be the bearer of bad tidings, but there is NO reliable way to do what you're attempting. malloc() may even have allocated more space than you asked for, but in any case there's no portable way to find out how much. You'll just have to keep track of the size of your malloc()ed objects yourself.