Re: Minimizing Dynamic Memory Allocation
On Jan 26, 4:26 pm, p...@lib.hu wrote:
And you miss the separation of responsibilities.
We have RAII/RIID/manager/etc classes -- that have
responsibility to manage a single resource. Examples:
auto_ptr, string, vector, CFile, CSingleLock, shared_ptr.
They normally reside in libraries ready to use. Ocasinally
there is some some really new resource to cover, but it is
more like exceptional.
Then there are the normal user classes this whole discussion
is about. They, instead of attempting to manage resources
directly shall use the classes mentioned before. As members,
base classes, locally, etc. You do that, and need not write
destructor (cctor, op= ) because cleanup is properly done.
Most real-life problems come when a user class rather starts
using raw pointers or raw resources, obviously messing up
somewhere the first 5+ times. And only to eventually discover
using a manager class.
Care to point out what I miss? ;-)
The fact that library classes only handle general solutions, and
rarely cover the exact case you need. As time goes on, your
personal library will become more an more complete. For your
specific application domain and programming style.
Some things are well covered by the library: if the size isn't
known at compile time, std::vector, std::string et al. seem to
cover most of the cases. If the type isn't known, then you can
consider boost::scoped_ptr, but this still requires that the
pointed to type be completely defined in the header file, where
as a simple pointer doesn't, and if there is only one such
pointer, it really doesn't buy us that much. And in many cases,
clean up will be more complicated than simply calling delete.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34