Problem on data location.

S

shuisheng

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.

Thanks,

Shuisheng
 
J

Jens Theisen

shuisheng said:
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.

There is no portable way of detecting whether an objects was allocated
by new or not, or whether it's automatic or not.

In you're case, I suggest you rethink your design, as this sounds
pretty wacky to me. If you post some example code one could help you a
bit more directly.

Regards,

Jens
 
G

Greg

shuisheng said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top