0

In my class I would like to have some dynamically allocated boolean array. I use shared_ptr to hold it:

boost::shared_ptr<bool[]> someBoolArray;

Memory allocation occurs in class constructor:

// someValue was read from file
someBoolArray = boost::shared_ptr<bool[]>(new bool[someValue]); 

Is it possible to set initial value for my array during shared_ptr initialization?
I want all values in array to be false by default.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
vard
  • 2,142
  • 4
  • 30
  • 40
  • Is zero-initialization ok, or does it have to be a specific, user-defined set of values? – jogojapan Jun 30 '13 at 11:54
  • I want all values in array to be false by default. – vard Jun 30 '13 at 11:57
  • You should use `boost::shared_array` for this. – jrok Jun 30 '13 at 12:00
  • shared_ptr does not work that way, see e.g., http://stackoverflow.com/questions/13061979/shared-ptr-to-an-array-should-it-be-used and http://stackoverflow.com/questions/8624146/c11-standard-scoped-array-wrappers. – Bishop Jun 30 '13 at 12:11

4 Answers4

4

If you want to guarantee the elements are set to false, you need an expression of the type

new T[N]();

In this case,

someBoolArray = boost::shared_ptr<bool[]>(new bool[someValue]()); 
//                                                           ^^
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

in C++11 you can pass initializer to array:

new bool[someValue] {false}

And I suggest you to use make_shared instead of your form.

Balog Pal
  • 16,195
  • 2
  • 23
  • 37
1

If you don't have C++11, you'll need to replace your bool array with a class that clears the array in its constructor, since you can only use the default constructor when allocating arrays of objects. This class should contain a bool* and have typecast operators to bool* and const bool* as well as operator [] overloaded.

Or you can give up on clearing the array at construction and add a for loop right after your someBoolArray = ... line to do the clearing. Either approach results in the same compiled code.

BTW, the inability to create arrays of objects with constructors other than the default constructor was a language defect addressed by C++11.

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • But you can at least call `new bool[N]()`, which will set all the elements to `false`. – juanchopanza Jun 30 '13 at 12:14
  • @juanchopanza Yes, that's what I had in mind when I asked about zero-initialization in the comment. Strangely, I can't get boost to accept this. Maybe my version isn't up to date enough. – jogojapan Jun 30 '13 at 13:11
0

shared_ptr does not work that way, see e.g., shared_ptr to an array : should it be used? and Does C++11 have wrappers for dynamically-allocated arrays like Boost's scoped_array?.

In answer to your question, a default constructed bool (as you get with new bool[4711]()) should have the value false already so you should be OK.

Community
  • 1
  • 1
Bishop
  • 188
  • 4
  • 1
    The question is about `boost::shared_ptr`, which behaves seems to have a specialization for arrays. Also, `new bool[N]` will not set the elements to `false`. – juanchopanza Jun 30 '13 at 12:15
  • Default constructor for `bool` is like for other integer types: it does nothing. You may be getting confused by the fact that a function-static or global bool array *is* initialized to 0, but that's a recent thing (I think it carries over from C, actually) and not required by the standard. – Mike DeSimone Jun 30 '13 at 12:18