home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16921 < prev    next >
Encoding:
Text File  |  1992-11-20  |  1.7 KB  |  60 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!portal!dfuller
  3. From: dfuller@portal.hq.videocart.com (Dave Fuller)
  4. Subject: Re: Memory Info.
  5. Message-ID: <By17Av.LqD@portal.hq.videocart.com>
  6. Organization: VideOcart Inc.
  7. X-Newsreader: Tin 1.1 PL3
  8. References: <1992Nov20.130913.20148@sifon.cc.mcgill.ca>
  9. Date: Fri, 20 Nov 1992 20:23:18 GMT
  10. Lines: 48
  11.  
  12. vugranam@pike.ee.mcgill.ca (Vugranam Chakravarthy Sreedhar) writes:
  13. : Given a pointer to a object that has been previously allocated using
  14. : one of the allocation routine, how can I get the
  15. : size of the object.
  16. : For example,
  17. : char *c;
  18. : long  x ; 
  19. :  c = (char *) malloc (100) ;
  20. :     .
  21. :     .
  22. :     .
  23. : x = mem_size(c) ;   /* x is block size of c, which in this case is 100 */
  24. : I want to use this to keep track of memory statistics. Now what I do is
  25. : to pass the size to my memory routines. Like
  26. : int *mymalloc(size) ;  /* this is easy */
  27. : int myfree(char *, long size)   /* I don't like this !!*/
  28. : Thanks
  29. : Sreedhar
  30.  
  31.    Try using a static array of structures that have the pointer and the size
  32. of it in each structure member. Then when you pass the pointer to the routine
  33. that returns the size of the area pointed to, all you have to do is search
  34. the array for the pointer and return the value associated with it.
  35.  
  36. BTW, myfree() is useless. When you free a pointer you don't need to give
  37. the size. Unless this routine subtracts the size from the current total
  38. of memory allocated.
  39.  
  40. Try the structure array, make it static in the module, and make the module
  41. contain only the my_malloc, my_free, and maybe a routine called my_mem_usage
  42. that will total up the values in the array and return it.
  43.  
  44. dave fuller  
  45. dfuller@portal.hq.videocart.com
  46.  
  47.  
  48.