CppLearner
CppLearner

Reputation: 17040

initialize dynamic array elements to zeros

For a fixed array,

  // will initialize the missing elements to 0 as well
   A[max_row][max_col] = {0,} 

Can we achieve this in dynamic arrays (multidimensional, in particular)?

Side question: if we can't, and we are forced to use nested loop, then how does the initialization time of the trick above compared to nested loop initialization?


I don't want to vector, otherwise this question is meaningless. Thanks for the advise :)

Upvotes: 2

Views: 7814

Answers (1)

GManNickG
GManNickG

Reputation: 504313

If you do this: new int[N]() /* note parenthesis */, then they are all zero initialized.

You should really use a std::vector, though.

Upvotes: 12

Related Questions