home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!microsoft!hexnut!jimad
- From: jimad@microsoft.com (Jim Adcock)
- Subject: Re: Operator "new" - how to catch array-allocation?
- Message-ID: <1992Dec18.192307.1841@microsoft.com>
- Date: 18 Dec 92 19:23:07 GMT
- Organization: Microsoft Corporation
- References: <1992Dec14.143339.26314@daimi.aau.dk> <1992Dec17.131239.8036@daimi.aau.dk>
- Lines: 49
-
- In article <1992Dec17.131239.8036@daimi.aau.dk> sabroe@daimi.aau.dk (Morten Sabroe Mortensen) writes:
- |The reason I raised this in the first place is, I'm about to build my own
- |special-purpose-heap-manager, -the way I would like it to work, is to attach
- |"something" to a given class, and then the result should be, that all dynamic
- |allocation involving that class should be managed - the way you allocate
- |should be independent of how heap-management is controlled - controlled by
- |the normal general-purpose manager or a special-purpose manager!
- |As I see it, -now-, I can't get this independency - except when allocating
- |single instances of the used class! Right?
-
- You can do it now -- you just can't do it by overloading the standard syntax,
- because "today"s compilers follow the rule that allocating new arrays
- uses the global allocator, and thus is not an issue under class control.
-
- As examples of approaches you could use today that don't use standard
- syntax, consider the following:
-
- You could provide a static member function to allocate your arrays:
-
- Foo* arrayOfFoo = Foo::NewArray(1000);
-
- where NewArray could, for instance, be implemented using malloc and then
- the objects of the array init'ed using placement operator new on each element.
-
- Note then you need to provide a compatible method for deleting your arrays:
-
- Foo::DeleteArray(arrayOfFoo);
-
- or:
-
- DeleteArray(arrayOfFoo);
-
- or something.
-
- An alternate [even more hack IMHO] approach you see people using occasionally
- is to overload operator new using "placement" syntax:
-
- Foo* arrayOfFoo = new (1000) Foo;
-
- where inside of this "placement" you know the size of the object, you
- know how many objects -- you just don't necessarily know what Ctors should
- be invoked on each element, since Foo could be derived from, meaning
- that this overloaded new would need to be reimplemented in each deriving
- class. Also, given that this new looks to the compiler like an allocator
- for one element, the compiler will automagically init the first element
- of the array, so one had better not do that inside the placement new.
- One still has the issue then how to deallocate this thing....as you
- might guess I don't recommend this approach.
-
-