2

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 :)

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
CppLearner
  • 16,273
  • 32
  • 108
  • 163
  • why not use vector< vector > for dynamic 2D arrays which you can initialize with default values ? – Arunmu Nov 27 '11 at 05:14
  • 1
    I know you can do that. Everyone wants to use vector, but I don't want to. I am sorry I forgot to include that. Thanks though. – CppLearner Nov 27 '11 at 05:14
  • 1
    Regarding your first question: What happened when you tried? Regarding your second question: Internally C++ represents a multi-dimensional array as a single flat list of memory locations of type "whatever" (where whatever is how you invoked new). You don't need a nested for loop if you do it that way. You can just use a pointer to run through row x col number of positions with a single loop. – DavidO Nov 27 '11 at 05:17
  • 1
    Why don't you want to? Just for understanding (practice), or for real use? – GManNickG Nov 27 '11 at 05:18
  • 1
    Exact Duplicate of [How do you initialise a dynamic array in C++?](http://stackoverflow.com/questions/2029651/how-do-you-initialise-a-dynamic-array-in-c) – Alok Save Nov 27 '11 at 05:20
  • 1
    ok. In that case I am sure you are creating your 2D array using malloc/new..right ? In that case you can value initialize with new operator or use memset for malloc. Eg "new int[ROWS]()" witll value initialie to zero – Arunmu Nov 27 '11 at 05:20
  • @DavidO. Thanks for the response. It's clear. – CppLearner Nov 27 '11 at 05:20
  • @Als Thanks. Sort of (or maybe fully yes). The original poster worked on char, and I am working on numbers so at first I thought it would be different (for example, using memset). – CppLearner Nov 27 '11 at 05:22
  • @GMan Yes. Sometime vector can introduce overheads that we don't want (maybe not). I am actually running a benchmark on several arrays so that's why I need a version on dynamic array. – CppLearner Nov 27 '11 at 05:24
  • 3
    @CppLearner: No. The overhead of a `std::vector` (if it even exists at all, which I believe it doesn't) is absolutely trumped by the gains in maintainability and program correctness. – GManNickG Nov 27 '11 at 05:27
  • 1
    @CppLearner: Welcome. In case you are doing some extensive benchmarking between the use of arrays and vectors..you might want to post your results in the FAQ section. And I agree completely with GMAN with his above comment – Arunmu Nov 27 '11 at 05:30
  • Yes. I am pretty sure mine is small. I do plan on performing a research on this type of stuff in the upcoming semester. Thanks guys. – CppLearner Nov 27 '11 at 05:31
  • 1
    I get nervous when I see "is absolutely trumped by". Of course it remains to be verified whether or not there is any measurable overhead when using vectors as opposed to arrays, but if there IS, such an absolute assertion is begging to find an exception. – DavidO Nov 27 '11 at 05:37
  • @DavidO: It's silly to think I'm speaking in "true" absolutes; we aren't in Philosophy 101 any longer. :) – GManNickG Nov 27 '11 at 05:49
  • @GMan: Thank Socrates we're not in Philosophy 101. :) Anyway, great answer below. – DavidO Nov 27 '11 at 05:54
  • @GMan: I think we showed that vecter has no overhead in comparison to arrays here: http://stackoverflow.com/q/3664272/14065 – Martin York Nov 27 '11 at 06:03

1 Answers1

12

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

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

GManNickG
  • 494,350
  • 52
  • 494
  • 543