How to find whether Object is in HEAP/Stack.

B

balu

Hi Gurus
I need a help. Im developing a C++ libaray. I don't
whether user creates object in stack/heap. Is there a
way to find object location?

Balu.N
 
H

hacker++

balu said:
Hi Gurus
I need a help. Im developing a C++ libaray. I don't
whether user creates object in stack/heap. Is there a
way to find object location?

Balu.N

Why do you care?
 
G

Greg

balu said:
Hi Gurus
I need a help. Im developing a C++ libaray. I don't
whether user creates object in stack/heap. Is there a
way to find object location?

Balu.N

There is no facility in C++ itself to provide this information. There
are various ways a program can try to capture this information when the
object is allocated. But even this approach can be more difficult than
it looks. There are also non-portable ways of finding an object's
location, ususally involving pointer comparisons.

More often than not, though, there is a better question to ask than
this one. After all if a client can instantiate an object of a
particular class either on the heap or on the stack, then the class
should have little reason to care which location the client picked.

On the other hand, if the location does make a difference to the class,
it is often better for the class to allow allocation in only one type
of location. For instance, ref-counted objects almost always have to be
allocated on the heap. For a class of this type, allocating on the
stack would either make no sense or would require a behavior so
different from heap-based objects of the same class - that permitting
both would just be too confusing. In this case, it would be perfectly
appropriate for the class to enforce heap-based allocation, usually
through a factory method and a private constructor.

Without knowing more about the background of this question, I cannot
suggest what the better question might be. But I think it quite likely
that a better question does exist.

Greg
 
H

hacker++

balu said:
Hi Gurus
I need a help. Im developing a C++ libaray. I don't
whether user creates object in stack/heap. Is there a
way to find object location?

Balu.N

Why do you care?
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top