From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14539 invoked by alias); 30 Oct 2002 14:29:00 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 14455 invoked from network); 30 Oct 2002 14:28:58 -0000 Received: from unknown (HELO smtp-relay-3.sea.adobe.com) (192.150.22.10) by sources.redhat.com with SMTP; 30 Oct 2002 14:28:58 -0000 Received: from inner-relay-3.corp.adobe.com (inner-relay-3 [153.32.251.51]) by smtp-relay-3.sea.adobe.com (8.12.3/8.12.3) with ESMTP id g9UERkOi018784 for ; Wed, 30 Oct 2002 06:27:47 -0800 (PST) Received: from iplan-mn.corp.adobe.com (iplan-mn.corp.adobe.com [130.248.25.5]) by inner-relay-3.corp.adobe.com (8.12.3/8.12.3) with ESMTP id g9UEP48d021723 for ; Wed, 30 Oct 2002 06:25:04 -0800 (PST) Received: from mn-eljaypc.adobe.com ([130.248.188.115]) by iplan-mn.corp.adobe.com (Netscape Messaging Server 4.15 mn Jul 11 2001 16:32:57) with ESMTP id H4STK600.N6R; Wed, 30 Oct 2002 08:28:54 -0600 Message-Id: <4.3.2.7.2.20021030082146.036212e8@iplan-mn.corp.adobe.com> X-Sender: eljay@iplan-mn.corp.adobe.com Date: Wed, 30 Oct 2002 06:29:00 -0000 To: Arunachalam G , gcc-help@gcc.gnu.org From: Eljay Love-Jensen Subject: Re: why there is no memlen(); In-Reply-To: Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-SW-Source: 2002-10/txt/msg00371.txt.bz2 Hi Arun, >is there any way to get the amount of memory allocated to a pointer. Depends entirely on your heap manager. For example, one platform that I worked on, you could do this: typedef unsigned char byte; struct MemChunk { unsigned long length; // 32-bit byte data[1]; // stretchy buffer }; unsigned long GetMemLength(void* p) { if(p == NULL) return 0; struct MemChunk* mem = (struct MemChunk*)((byte*)p - sizeof(unsigned long)); return mem->length; } void Test() { void* p = malloc(1000); printf("%ld allocated\n", GetMemLength(p)); free(p); } However, this trick is VERY platform (OS + Compiler) specific. I'm not sure what GCC uses. Even if something akin to the above works for GCC, it may be OS specific and/or version specific! Caveat emptor! --Eljay