shuisheng wrote:
Dear All,
I am wondering if there is a way to know if a data is in heap? My case
is like this:
I have a data and want to shallowly copy it into an object (just pass
its head pointer). If the data is in heap, the object will take charge
of deleting it.
Making a shallow copy of a stack-allocated object does not strike me as
a particularly good idea. Frankly, it's an accident waiting to happen.
So it sounds as if there perhaps should be two types of copy methods
offered by the class's interface - one shallow and one deep. In that
case, it would be up to the client of the interface to choose the
appropriate copy method.
Alternately, the object's class interface can simply make it a
precondition of the copy routine that the object to be copied must have
been dynamically allocated. Such a precondition could even be enforced
on a class-by-class basis by implementing a class whose instances
cannot be allocated on the stack.
Greg