0

I have struct Node in my code defined like this:

struct Node {
Node() : prev(0), left(0), right(0) {}
Node* prev;
Node* left;
Node* right;
char val;
};

I initialize array of Node structer objects like that:

Node** pool = new Node*[10000000];

I thought it will create Node structure objects using defoult constructor, but in fact the arrays seems to be empty. Is there any EFFICIENT way to create immidiately array of 'empty' objects?

3
  • 2
    You're not creating Node objects in that line, you're creating Node* (all of which (outside of Debug) are likely to point to random bits of memory). Commented Apr 27, 2017 at 15:54
  • 4
    you never want to use 'new' within a C++ program. Commented Apr 27, 2017 at 15:54
  • You will need to profile accessing your node pool versus allocating nodes from dynamic memory. IMHO, the performance gain from using a node pool is insignificant. Usually node pools are used by memory constrained systems like embedded systems. Commented Apr 27, 2017 at 16:04

3 Answers 3

5

You should be using std::vector<>; there is very little reason to use new in C++.

 auto pool = std::vector<Node>(10000000);

(Your original code is creating Node*s, not Nodes.)


If for some reason you don't want to use vector, you could use std::unique_ptr<>. That code would be

std::unique_ptr<Node[]> pool(new Node[10000000]);

but you really should use vector.


Also, you can simplify and improve Node:

struct Node final {
Node* prev = nullptr;
Node* left = nullptr;
Node* right = nullptr;
char val = '\0';

Node(const Node&) = delete;
Node& operator(const Node&) = delete;
};
Sign up to request clarification or add additional context in comments.

2 Comments

i upvote Dan's answer. Anyway, what are you trying to do? it looks like you are trying to create an xml parser. Check out on the web for existing libraries. i.sstatic.net/hUjpw.png
There are plenty of reasons to use "new" in C++. I work on projects where we don't even use the standard library for many reasons...
0

If you are constructing an ast/asg and you do not need linear data structures, you may consider still bulk allocating stuff with an ::std::vector<>, which will potentially give you cache locality.

You can still link individual Node objects via the pointers in your struct. If you then use indices instead of pointers, you can even resize your vector whenever you run out of available objects in your pool.

Comments

0
Node** pool = new Node*[10000000];

constructs an array of Node* rather than Node. What you are looking for is,

Node* pool = new Node[10000000];

which constructs an array of Node. If you actually want an array of Node* then the appropriate initialisation is,

int nObjects = 10000000;
Node** pool = new Node[nObjects];

for(auto i = 0; i < nObjects; ++i) {
    pool[i] = new Node();
}

As others have said you likely do not want to use new or C-style arrays if you can avoid it.

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.