Reputation: 71
This is a node for B+ tree. I wanted to use smart pointers as my program is leaking a lot of memory. How can convert the code by using smart pointers ?
class node
{
public:
long* key;
int capacity;
node** nodes;
node* parent;
long* value;
node ( int order ) {
key = new long[order + 1];
value = new long[order + 1];
nodes = new node *[order + 2];
capacity = 0;
parent = NULL;
for ( int i = 0; i <= order + 1; i++ ) {
this->nodes[i] = NULL;
}
}
~node() {
delete[] key;
delete[] value;
for ( int i = 0; i <= order + 1; i++ ) {
delete nodes[i];
}
}
};
Upvotes: 0
Views: 1885
Reputation: 477338
Don't use smart pointers. Be smart and don't use pointers, but rather containers:
#include <vector>
struct node
{
std::vector<long> keys;
std::vector<long> values;
std::vector<node *> nodes;
node * parent;
};
Depending on the constraints of your structure I might even wish to make parent
a node &
(if the parent always comes first and never changes), or maybe a std::reference_wrapper<node>
. But that's a minor quibble.)
Upvotes: 5