Stack Object

How can I dermine if a class is dynamic memory or in the stack?

The class is a pure virtual class (I want to tell if the derived classes are stack object)
Tomb64Asked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
I agree and dissagree.

There are ways that are not platform specific, but usually aren't great.  Most (all?) of these techniques revolve around overloading the new (and maybe delete) operator for the class involved.  The new operator can then do "things" to help track the fact that the object is dynamcicllly allocated.  For example, it may store a pointer to the object in a container of some sort (hash table?).  This table could later be searched for the address to see if the object was dynamically allocated.  The overloaded delete operator would have to remove the object from the table.)  

This sort of approach is a little dangerious though.  There are usually things that a program can do to "get around" the mechanism anc cause it to work improperly, thus it is not fool proof--because fools are so ingenius.  It is usually a better idea to just redesign your program so that this sort of information (whethor or not an object is allocated dynamically) is not needed.

Another solution is to prevent objects from a class from every being dynamically allocated or requiring that they always be dynamically allocated.  There are techniques for these features that are a little safer to use (harder for a programmer to circumvent.)
0
 
mikeblasCommented:
There's no provision in C++ for this.  You can cobble together something platform-specific, though.  We can't help you with that until we know what version of which compiler you're using on what version of which platform.

..B ekiM
0
 
jkrCommented:
mikeblas is correct. The only method that comes into my mind applies to MS VC++. It has a '_CrtIsValidHeapPointer()' which could serve this purpose...
0
 
mbormannCommented:
I dont knwo much C++,but continuing in the same line as nietod ,in the last paragraph where he has explained ,can be found in Scott Meyer's book 'More Effective C++' (or is it in 'Effective C++' ,i am not sure)

thanks to nietod for his excellent suggestion ...
0
All Courses

From novice to tech pro — start learning today.