1

I have this:

struct Node;
typedef boost::intrusive_ptr<Node> NodeSPtr;

...

boost::scoped_array<NodeSPtr> nodes(new NodeSPtr[size]);

...

// "pollute" operations ...

...

// reset all the items in the array
for (size_t i = 0; i < size; ++i)
    nodes[i].reset();

What is the cleanest more STLish way to initialize the array. Note that the code is performance sensitive and using vector is not an option.

gsf
  • 6,612
  • 7
  • 35
  • 64
  • 2
    I'm not entirely sure what you want. It seems like you just want to reset the `scoped_array`. There is a [Reset()](http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/scoped_array.htm) function in `boost::scoped_array`. – Steve Van Opstal Dec 25 '13 at 18:58
  • scoped_array reset frees all the items in the array it does not reset them – gsf Dec 25 '13 at 19:36

1 Answers1

0

The default constructor of intrusive_ptr has a postcondition that get() == 0 according to the docs. So to default-construct the array values, just stick a pair of curly braces (uniform initialization) after the new, like so:

boost::scoped_array<NodeSPtr> nodes(new NodeSPtr[size]{});

You can also use parentheses if you cannot use the uniform initialization syntax.

ECrownofFire
  • 472
  • 4
  • 11
  • the initial initialization is OK. The question is hoe to reset the array to NULL pointers at some other point – gsf Dec 25 '13 at 20:28
  • I see, I don't think there's really a good way to do that other than a `for` loop like you already said. Could also try a `std::fill_n` or something if you want it to look a bit nicer. – ECrownofFire Dec 25 '13 at 21:49