0

I am working on a project, where I need to generate a large Tree and delete it afterwards over and over again. But somehow the memory is not being freed after being deleted, so more and more memory is being used instead of reusing memory. Each node can have a different amount of children. I have tried implementing smart pointers, but I was unsuccessful. Is my tree being deconstructed properly or am I missing something?

class Node {
    public:
        short value;
        std::vector<Node*> children;

    virtual ~Node()
    {
        for (auto p : children) {
            delete p;
        }
    }
}
Moritz03
  • 3
  • 3
  • how do you measure "the memory is not being freed after being deleted" ? `delete` does free the memory, but I suppose you are looking at the memory given by your OS to the process, thats something slightly different – 463035818_is_not_an_ai Mar 25 '20 at 15:07
  • The C++ memory manager usually doesn't release memory back to the OS immediately. OS allocation is costly and your program is most likely going to do more allocations, so it make sense to hold on to it for later. – molbdnilo Mar 25 '20 at 15:18
  • 2
    please provide a [mre]. https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three/4172724 is a likely possibility – Alan Birtles Mar 25 '20 at 15:30
  • 1
    why `std::vector`? why not simply `std::vector`? – Adham Zahran Mar 25 '20 at 15:50

0 Answers0