home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!portal!dfuller
- From: dfuller@portal.hq.videocart.com (Dave Fuller)
- Subject: Re: Memory Info.
- Message-ID: <By17Av.LqD@portal.hq.videocart.com>
- Organization: VideOcart Inc.
- X-Newsreader: Tin 1.1 PL3
- References: <1992Nov20.130913.20148@sifon.cc.mcgill.ca>
- Date: Fri, 20 Nov 1992 20:23:18 GMT
- Lines: 48
-
- vugranam@pike.ee.mcgill.ca (Vugranam Chakravarthy Sreedhar) writes:
- :
- : Given a pointer to a object that has been previously allocated using
- : one of the allocation routine, how can I get the
- : size of the object.
- :
- : For example,
- :
- : char *c;
- : long x ;
- :
- : c = (char *) malloc (100) ;
- : .
- : .
- : .
- :
- : x = mem_size(c) ; /* x is block size of c, which in this case is 100 */
- :
- :
- : I want to use this to keep track of memory statistics. Now what I do is
- : to pass the size to my memory routines. Like
- :
- : int *mymalloc(size) ; /* this is easy */
- :
- : int myfree(char *, long size) /* I don't like this !!*/
- :
- : Thanks
- :
- :
- : Sreedhar
-
- Try using a static array of structures that have the pointer and the size
- of it in each structure member. Then when you pass the pointer to the routine
- that returns the size of the area pointed to, all you have to do is search
- the array for the pointer and return the value associated with it.
-
- BTW, myfree() is useless. When you free a pointer you don't need to give
- the size. Unless this routine subtracts the size from the current total
- of memory allocated.
-
- Try the structure array, make it static in the module, and make the module
- contain only the my_malloc, my_free, and maybe a routine called my_mem_usage
- that will total up the values in the array and return it.
-
- dave fuller
- dfuller@portal.hq.videocart.com
-
-
-