-1

Is there functionality, in C++, to detect whether the target of a pointer has been allocated from dynamic memory or is living in static memory (including global memory)?

In the example below, I'd like to have the Destruction function only call delete if the target of the pointer resides in dynamic memory. (This is a minimized version of the actual code.)

#include <iostream>
using std::cout;

void Destruction(int * p_variable)
{
    // The statement below should only be executed
    //    if p_variable points to an object in dynamic memory.
    delete p_variable;
}

int main()
{
    cout << "Deletion test.\n";
    static int static_integer = 5;
    int * p_dynamic_integer = new int;
    *p_dynamic_integer = 42;

    // This call will pass
    Destruction(p_dynamic_integer);

    int * p_static_integer = &static_integer;
    // Undefined behavior???
    Destruction(p_static_integer);

    cout << "\nPaused.  Press Enter to continue.\n";
    cout.ignore(10000, '\n');
    return 0;
}

The basis of the question is that I'm getting an exception error in the Destruction function because the pointer is pointing to a static integer.

The design is that a Factory Pattern function returns a pointer to a base class. The pointer could be pointing to a child that is a singleton (static memory) or an child instance from dynamic memory. A function is called to close the program (think closure of a GUI) and if the target of the pointer is from dynamic memory, it needs to be deleted.

I'm looking for a solution where I don't have to worry about deleting or not deleting the target of the pointer.

5
  • Isn't it a bit weird to have your factory that can return a pointer to singleton ? It may be mandatory in your application but it seems a bit weird ;) What you could do even if it is a bit ugly is to compare your pointer with your singleton address and delete only if it differs Commented Dec 4, 2020 at 23:39
  • Nope, not weird. The program is running test fixtures. The factory returns a pointer to a test fixture instance. The test fixture instance may be a singleton or it may be from dynamic memory; difficult to enforce as the program is adapted to different kinds of test fixtures. Commented Dec 4, 2020 at 23:43
  • 1
    While this can probably be done by looking at where static memory gets mapped in the address space, it'd be brittle (do you have thread_locals?). Can't you return a smart pointer from the factories that also carry a bool in them that signals whether it's owned or not? Commented Dec 4, 2020 at 23:56
  • 2
    There is no "one right way" to do this. You can do it however you want. One obvious way is a wrapper class that includes the inner object and the appropriate way to destroy it (which may be a no-op). You can use std::shared_ptr for this purpose too, if you want. Or you can have a registry of static objects and you check if the object's address is in that registry. You really can implement this however you want, but you do have to do it. Commented Dec 5, 2020 at 0:02
  • @DavidSchwartz: Interesting concept: Have the Factory destroy the object since it would know or be closer to know how the object was allocated. Commented Dec 5, 2020 at 0:08

1 Answer 1

3

As far as I know, there's at least no clean/robust way to do that. Maybe there are some further heuristic approaches/hacks like the ones here:

static and dynamic memory allocation of objects in C++

But as another user already mentioned there: If you really have to distinguish the memory place type, which might be questioned in terms of design at least (there are dozends of singleton concepts that cover some of the aspects here), you could provide this information explicitly for the used class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.