In C++ Good reasons for NOT using symmetrical memory management (i.e. new and delete)
- by Jim G
I try to learn C++ and programming in general. Currently I am studying
open source with help of UML. Learning is my hobby and great one too.
My understanding of memory allocation in C++ is that it should be
symmetrical. A class is responsible for its resources. If memory is
allocated using new it should be returned using delete in the same
class. It is like in a library you, the class, are responsibility for
the books you have borrowed and you return them then you are done.
This, in my mind, makes sense. It makes memory management more
manageable so to speak.
So far so good. The problem is that this is not how it works in the
real world.
In Qt for instance, you create QtObjects with new and then hand over
the ownership of the object to Qt. In other words you create QtObjects
and Qt destroys them for you. Thus unsymmetrical memory management.
Obviously the people behind Qt must have a good reason for doing this.
It must be beneficial in some kind of way,
My questions is:
What is the problem with Bjarne Stroustrups idea about a symmetrical
memory management contained within a class?
What do you gain by splitting new and delete so you create an
object and destroy it in different classes like you do in Qt.
Is it common to split new and delete and why in such case, in other
projects not involving Qt?
Thanks for any help shedding light on this mystery!