471,075 Members | 716 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

How to find whether Object is in HEAP/Stack.

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

Aug 9 '05 #1
3 2901
balu wrote:
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?

Aug 9 '05 #2
balu wrote:
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

Aug 9 '05 #3
balu wrote:
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?

Aug 9 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by August1 | last post: by
11 posts views Thread by Dan Elliott | last post: by
13 posts views Thread by Abe Frohnman | last post: by
1 post views Thread by Geiregat Jonas | last post: by
3 posts views Thread by nahur | last post: by
7 posts views Thread by Arpan | last post: by
53 posts views Thread by fdmfdmfdm | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.