home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13328 < prev    next >
Encoding:
Internet Message Format  |  1992-09-07  |  2.5 KB

  1. Path: sparky!uunet!igor!thor!rmartin
  2. From: rmartin@thor.Rational.COM (Bob Martin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ Memory Management
  5. Keywords: Memory, Heap, new, delete
  6. Message-ID: <rmartin.715700914@thor>
  7. Date: 5 Sep 92 13:48:34 GMT
  8. References: <37674@sdcc12.ucsd.edu>
  9. Sender: news@Rational.COM
  10. Distribution: ca
  11. Lines: 44
  12.  
  13. acanter@sdcc3.ucsd.edu (Adriaan Canter) writes:
  14.  
  15.  
  16. |Hello, I've noticed that Borland Turbo C++ (v 1.0) will actually reserve 16
  17. |bytes when the "new" function is used to allocate space for a 2 byte
  18. |integer, i.e. the following code:
  19.  
  20. |int * intptr;
  21. |intptr = new int;
  22.  
  23. |will allocate 16 bytes, as measured using the "coreleft()" function,
  24. |under the small memory model. A subsequent second allocation of this type will
  25. |fit the second integer into the first 16 byte space allocated, but a third call
  26. |will block off an additional 16 bytes, and so on, so that 4 two-byte integers
  27. |will take up 32 bytes of space, for example. I guess this happens because some
  28. |space is reserved to point to the next location in the heap, although the
  29. |amount seems excessive. This is pretty wasteful of course, if I want to
  30. |dynamically allocate alot of structures. Is there anyway I can control how
  31. |much memory is actually reserved when the "new" function is called? Do the
  32. |newer versions of C++ have some memory managment classes (as I've over-heard)
  33. |that can fix this? Or do I have to finagle my own scheme, overloading the
  34. |"new" and "delete" operators. Forgive me if this is a dumb question, but my
  35. |manuals don't say very much about it. Thank you.
  36.  
  37. Some minimal amount of storage overhead must be incurred by the
  38. general memory management schemes.  In the case you describe above,
  39. this overhead comes to 6 bytes.  That's not too bad.  It amounts to a
  40. pointer and a size...
  41.  
  42. If you want to decrease your overhead below the empirical 6 bytes,
  43. then you must concoct your own memory management scheme.  This is
  44. easiest to do if the objects you want to allocate are all the same
  45. size.  You can overload operator new for that class, and then allocate
  46. large arrays of storage.  Two arrays will do.  One serves as that
  47. actual storage that you will dish out with your overloaded new, and
  48. the other array can act as a bitmap to record whether a cell in your
  49. array is free or not.  This amounts, theoretically, to a one bit
  50. overhead per block.
  51.  
  52. --
  53. Robert Martin                        Training courses offered in:
  54. R. C. M. Consulting                       Object Oriented Analysis
  55. 2080 Cranbrook Rd.                        Object Oriented Design
  56. Green Oaks, Il 60048 (708) 918-1004       C++
  57.