How to safely delete something not dynamically allocated

If one tries to delete a pointer that isn't pointing to dynamically allocated memory, is there any way to call delete safely? For instance, I thought it would just throw an exception, in which case I could just use a try-catch block. I tried that, and

1
2
3
4
5
6
7
try
{
    int a;
    int* p = &a;
    delete p;
}
catch(...){}


didn't work.
As far as I know this cannot be done in any reliable/portable way.
If you didn't explicitly allocate it, why would you have the right to explicitly free it?
Exactly. I was planning to have an object which contained an array of pointers to a certain kind of object, some of which would be statically allocated, some of which would be dynamically allocated, but which ones wouldn't be known until runtime, and the destructor would use a loop to delete each member that was dynamically allocated. Thanks for your replies!
You could try to make some kind of smart pointer type that allows you to indicate if its not dynamically allocated.
Topic archived. No new replies allowed.