12

In the book I am reading at the moment (C++ Complete Reference from Herbert Schildt), it says that no array allocated using new can have an initializer.

Can't I initialize a dynamically allocated array using new? If not whats the reason for it?

Mat
  • 202,337
  • 40
  • 393
  • 406
haris
  • 2,003
  • 4
  • 25
  • 24
  • 4
    No matter whether he's correct on this one or not, trash that book right away. Schildt's books have a very glorious reputation for being incredibly wrong. Really, I'm not joking, he is famous among the community for his books being so bad they stand out among the very many really bad C++ books. There's memes about him. For good C++ books have a look at [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/140719) instead. – sbi Jul 16 '11 at 12:21

3 Answers3

19

That's not quite true (you should almost certainly get yourself an alternative reference), you are allowed an empty initializer (()) which will value-initialize the array but yes, you can't initialize array elements individually when using array new. (See ISO/IEC 14882:2003 5.3.4 [expr.new] / 15)

E.g.

int* p = new int[5](); // array initialized to all zero
int* q = new int[5];   // array elements all have indeterminate value

There's no fundamental reason not to allow a more complicated initializer it's just that C++03 didn't have a grammar construct for it. In the next version of C++ you will be able to do something like this.

int* p = new int[5] {0, 1, 2, 3, 4};
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • @MikeDeSimone: Each expression in the initializer list is used to initialize the elements of the new array in order. – CB Bailey Jul 16 '11 at 12:40
  • So is it limited to single-parameter constructors, or is there some special syntax for multi-parameter constructors? For example, if I want to initialize a coefficient table of four `complex`s to 1, j, -1, and -j (where `j == complex(0, 1)`), how would that look? – Mike DeSimone Jul 16 '11 at 12:47
  • @MikeDeSimone: Something like: `new std::complex[4] = { std::complex(1, 0), std::complex(0, 1), /* ... */ };` – CB Bailey Jul 16 '11 at 12:58
  • 2
    @MikeDeSimone: Actually, it's simpler, you should be able to do: `new std::complex[4] = { {1, 0}, {0, 1}, {-1, 0}, {0. -1} };` . – CB Bailey Jul 16 '11 at 13:02
  • thanx for the answer it was really helpfull...when i treid int* p = new int[5](); the array elements were not initialized to zero instead they were having indeterminate values...i checked this out in vc++6.0 is it the vc++ 6.0's problem?? – haris Jul 17 '11 at 08:57
  • @haris: Yes, this was fixed in VS .NET 2003, IIRC. Visual C++ 6.0 predates the current standard and is non-conforming in a fair number of ways. You should upgrade if possible. – CB Bailey Jul 17 '11 at 09:15
0

The book is correct; you cannot have,

int *p = new int[3](100);

There is no understandable reason behind it. That's why we have initializers for array in C++0x.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • I'm glad they fixed it. One of my eternal annoyances with C++ is how the embedded community doesn't seem to exist. With the new C++11 initializer syntax, we can finally have in-ROM arrays of objects by declaring them `const` (props for `constexpr` too). `std::vector` doesn't work for us because it allocates from the heap, i.e. RAM, which is usually in far shorter supply than flash, and has to get initialized from flash somehow anyway. – Mike DeSimone Jul 17 '11 at 02:11
0

I think the book is correct, in generally you cannot do that with current version of c++. But you can do that with boost::assign to achieve a dynamic array, see below

#include <boost/assign/list_of.hpp>
class Object{
public:
    Object(int i):m_data(i){}
private:
    int m_data;
};

int main()
{
    using namespace boost::assign;
    std::vector<Object> myvec = list_of(Object(1))(Object(2))(Object(3));
}
RoundPi
  • 5,819
  • 7
  • 49
  • 75