home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / cplus / 18282 < prev    next >
Encoding:
Text File  |  1992-12-22  |  2.5 KB  |  60 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!microsoft!hexnut!jimad
  3. From: jimad@microsoft.com (Jim Adcock)
  4. Subject: Re: Operator "new" - how to catch array-allocation?
  5. Message-ID: <1992Dec18.192307.1841@microsoft.com>
  6. Date: 18 Dec 92 19:23:07 GMT
  7. Organization: Microsoft Corporation
  8. References: <1992Dec14.143339.26314@daimi.aau.dk> <1992Dec17.131239.8036@daimi.aau.dk>
  9. Lines: 49
  10.  
  11. In article <1992Dec17.131239.8036@daimi.aau.dk> sabroe@daimi.aau.dk (Morten Sabroe Mortensen) writes:
  12. |The reason I raised this in the first place is, I'm about to build my own
  13. |special-purpose-heap-manager, -the way I would like it to work, is to attach
  14. |"something" to a given class, and then the result should be, that all dynamic
  15. |allocation involving that class should be managed - the way you allocate
  16. |should be independent of how heap-management is controlled - controlled by
  17. |the normal general-purpose manager or a special-purpose manager!
  18. |As I see it, -now-, I can't get this independency - except when allocating 
  19. |single instances of the used class! Right?
  20.  
  21. You can do it now -- you just can't do it by overloading the standard syntax,
  22. because "today"s compilers follow the rule that allocating new arrays
  23. uses the global allocator, and thus is not an issue under class control.
  24.  
  25. As examples of approaches you could use today that don't use standard
  26. syntax, consider the following:
  27.  
  28. You could provide a static member function to allocate your arrays:
  29.  
  30.     Foo* arrayOfFoo = Foo::NewArray(1000);
  31.  
  32. where NewArray could, for instance, be implemented using malloc and then
  33. the objects of the array init'ed using placement operator new on each element.
  34.  
  35. Note then you need to provide a compatible method for deleting your arrays:
  36.  
  37.     Foo::DeleteArray(arrayOfFoo);
  38.  
  39. or:
  40.  
  41.     DeleteArray(arrayOfFoo);
  42.  
  43. or something.
  44.  
  45. An alternate [even more hack IMHO] approach you see people using occasionally
  46. is to overload operator new using "placement" syntax:
  47.  
  48.     Foo* arrayOfFoo = new (1000) Foo;
  49.  
  50. where inside of this "placement" you know the size of the object, you
  51. know how many objects -- you just don't necessarily know what Ctors should
  52. be invoked on each element, since Foo could be derived from, meaning
  53. that this overloaded new would need to be reimplemented in each deriving
  54. class.  Also, given that this new looks to the compiler like an allocator
  55. for one element, the compiler will automagically init the first element
  56. of the array, so one had better not do that inside the placement new.
  57. One still has the issue then how to deallocate this thing....as you
  58. might guess I don't recommend this approach.
  59.  
  60.