It has already been explained that you will have to allocate objects dynamically (using new ), however this is fraught with dangers (memory leaks).
There are two (simple) solutions:
- Have a ownership scheme.
- Use the arena to host your sites and keep links to them.
1 Ownership scheme
In C and C ++, there are two forms of obtaining memory for storing an object: automatic storage and dynamic storage. What you use is automatically used when you declare a variable in your function, for example, however, such objects only live for the duration of the function (and, therefore, you have problems using them after that, because the memory is probably overwritten by something else). Therefore, you often have to use dynamic memory allocation.
The problem with dynamic memory allocation is that you must explicitly return it to the system so that it does not leak. In C, this is quite complicated and requires rigor. In C ++, although this has been simplified through the use of smart pointers. So let's use them!
struct Node { Node(Node* p, int k): parent(p), key(k) {} Node* parent; std::unique_ptr<Node> left, right; int key; };
Notice the different parent and left declaration? The parent owns its children ( unique_ptr ), while the child simply refers to its parent.
void insert(std::unique_ptr<Node>& root, const int key) { if (root.get() == nullptr) { root.reset(new Node{nullptr, key}); return; } Node* parent = root.get(); Node* y = nullptr; while(parent) { if(key == parent->key) exit(EXIT_FAILURE); y = parent; parent = (key < parent->key) ? parent->left.get() : parent->right.get(); } if (key < y->key) { y->left.reset(new Node{y, key}); } else { y->right.reset(new Node{y, key}); } }
If you do not know that unique_ptr , get() it simply contains the object selected with new , and the get() method returns a pointer to this object. You can also reset its contents (in this case, it correctly disposes of the object contained in it, if any).
I would notice that I'm not too sure about your algorithm, but hey, this is yours :)
2 arena
If it deals with memory, you have your own head all soft, this is pretty normal at first, and why sometimes arenas can be easier to use. The idea of using the arena is quite general; instead of worrying about owning the memory in parts, you use “something” to hold the memory and then manipulate the links (or pointers) to the parts. You just have to keep in mind that these links / pointers are only ever alive while the arena is.
struct Node { Node(): parent(nullptr), left(nullptr), right(nullptr), key(0) {} Node* parent; Node* left; Node* right; int key; }; void insert(std::list<Node>& arena, Node *&root, const int key) { arena.push_back(Node{});
Just remember two things:
- As soon as your arena dies, all of your links / pointers point to the ether, and bad things happen if you try to use them.
- If you ever just click things on
arena , it will grow until it consumes all available memory and your program will fail; at some point you need cleaning!