From: Philippe A. Bouchard (philippeb_at_[hidden])
Date: 2002-07-29 23:53:57
Ok. I've rewritten my previous proposal to draw aside unusual operator new
(size_t, ...) syntaxes by overloading the global operator new (size_t).
Pros & cons:
+ Fast smart pointer. Cannot be faster:
+ sizeof(ptr<int>) == sizeof(int *) (for copying);
+ one operator new (size_t) call to allocate the object (no counters).
+ Memory benefits:
+ sizeof(ptr<int>) == sizeof(int *);
+ sizeof(* ptr<int>) == sizeof(int) + 1;
+ thus maximises the number of pointers growth referring to one object.
+ Simple syntax:
+ no need for policy-based functions;
+ no need to inherit from counted_base;
+ no need for operator new (size_t, ...) like in my previous examples.
+ Doesn't matter if you don't use ptr<> to assign the allocated buffer. It
just takes a little bit more memory.
- Takes for granted that operator new (size_t) is not overloaded in specific
classes and if so, is not using direct malloc() calls. But it would be
possible to assert at compile-time it is not overloaded.
- It would begin to be more complex for low-level dynamic memory routines.
- This is why I am recommending boost::pool as the holding namespace.
Again the names are temporary. I would like your opinions first. Here is
statement examples as shown in testptr1.cpp:
ptr<child> p = new child("Bob");
ptr<child> q = new child("Rob");
ptr<child> r = new child("Tod");
q = p;
p = r;
cout << q->f << endl;
IMO it would be acceptable for new projects; it doesn't matter if you assign
the buffer to a raw pointer and use an explicit delete after. Easy to apply
on array allocations also.
Philippe A. Bouchard
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk