The question is pretty much that. In C++, if a pointer is not NULL
is there any way to determine if the data pointed was allocated on the heap (new
-type allocation) or on the stack (typical allocation and current scope lifetime).
I have an implementation of smart pointers and arrays (I know smart pointers exist in C++11 but I avoid them until there's a cleaner way to add smart arrays than currently) in which I keep track of reference count and so on. Whenever a pointer is not referenced at all anymore it is delete. Problem is, the current implementation doesn't prevent to give to the class a pointer to a variable on the stack (I do not want to force the creation of the pointer by the smart pointer, I have specific case where I want to do the allocation myself, for example when creating an array in a function and in the same function it needs a resize before being handed to the caller and so on), but if I give such a pointer, the class will try to call a delete
or a delete[]
on this pointer, which will result in undefined behavior (well let's be honest, a crash in most cases).
So is there a way that I check if I should or not delete this pointer on destruction or if the class should even accept it in the first place ?
Thanks in advance everyone.