19

Some programmers seem to violently hate them, while others seem to think they're fine. I know that anything that can be done to a multi-dimensional array can also be done to a regular array, so they're functionally equivalent. Is it bad practice to use multi-dimensional arrays, or does it not matter?

Maulrus
  • 1,787
  • 2
  • 17
  • 27
  • Consider boost::multi_array as an alternative. It solves some of the problems of arrays with less of the overhead and unwieldy syntax of nested vectors. – frankc Apr 21 '10 at 02:33

9 Answers9

12

Do you need to store multi-dimensional data where you know the dimensions ahead of time? If so, use a multi-dimensional array.

If you don't know the dimensions ahead of time (i.e., you're going to have to dynamically allocate the array), then you either need to either

  • allocate a one-dimensional array and emulate an n-dimensional array using index arithmetic, or
  • allocate an array of pointers to arrays of elements to get actual multidimensional array semantics

It depends on the specific use case, but as a rule of thumb, I almost always prefer the former because it makes for less memory management hassle and fewer heap allocations. The complexity for both approaches grows as the number of dimensions increases, but, in my opinion, it grows much faster for the latter approach due to the extra levels of indirection.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Also, emulating an n-dimensional array with a one-dimensional one can make your program run much faster if you traverse your data from rows by columns, because contiguous data will be loaded into the data cache. Whereas dynamically allocated multi-dimensional arrays will most likely get a higher cache-miss rate since data are probably stored at different locations. – hdl Nov 25 '15 at 17:47
4

Advantages of multi-dim arrays to Vector<Vector<>>

  1. Easy to type [ ][ ]
  2. C compatible.
  3. Simple to understand conceptually what is it is doing.

Disadvantages:

  1. No easily detected bounds checking. Bounding off the end of the outer brackets usually overflows into memory allocated by the inner brackets making these types of error a real pain to track.
  2. Jagged arrays require care to set up. Vector pattern is easy.
  3. Multidimensioal arrays are more than double pointers making it a pain to pass into functions correctly. Most of the time, I've seen them just passed as a raw address to a double pointer which defeats the intrinsic math the compiler will do for you.

Basically though, it comes down to lack of bounds checking for me.

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
  • 2
    re #1: `vector > a(1, vector(1)); a[0][0] = …` is OK. – Potatoswatter Apr 21 '10 at 02:27
  • Sure and I use that. Not as easy to type nor as intuitive as a[1][1]; though which I believe is a large reason why vector vectors aren't used as much as they should be. Heck, a typedef or macro (ugh) could probably mask the ugliness if needed. – Michael Dorgan Apr 21 '10 at 03:00
  • 2
    I would never use a `vector>` unless my use case was jagged. – Ben Voigt Apr 21 '10 at 03:17
1

There are following advantages of multi-dimensional arrays over Vector<Vector<>>:

  • They are easy to understand.
  • Searching and sorting of elements can be done very easily.
  • They are compatible with C.
  • They are easy to type.
zpea
  • 1,072
  • 6
  • 23
0

How would you implement my favorite algorithm without it?

ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
0

Well, in C++ I dislike multidimensional arrays because they should be replaced with std::vector<std::vector<t> >. They're also particularly important if you want to represent a std::vector<std::basic_string<t> >.

Multidimensional arrays are so simple a primitive I'm suprised most would care. However, a design that uses a single dimension is probably better than one using multiple dimensions, all other things being equal.

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
0

It may be possible to store multi-dimensional data in a single-data array, but you have to keep track of the indexes yourself. Multi-dimensional arrays are actually stored in memory as a single dimensional array, with syntax to support representing that data as multi-dimensional.

If you are working with multi-dimensional data, then I feel it is most appropriate to choose the correct tool for the job (a multi-dimensional array).

Matt
  • 112
  • 4
0

If multidimensional index computation bugs you, std::valarray with std::slice is the standard abstraction.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
0

I can recommend Boost.MultiArray. Boost.MultiArray provides a generic N-dimensional array concept definition and common implementations of that interface.

http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/index.html

Davit Siradeghyan
  • 6,053
  • 6
  • 24
  • 29
0

I know that anything that can be done to a multi-dimensional array can also be done to a regular array

I do not think that's entirely accurate. We'll need an array of pointers to store something as basic as a list of names and then sorting it. Or pointers to pointers to store a variable length string and then a list of such strings. As the original questions mentions only arrays per se, can't see how problems like these can be done with equal ease in a regular array. Please consider not just storing the strings in a 1-D array (using some sort of separator maybe) but also performing operations such as sorting.

AruniRC
  • 5,070
  • 7
  • 43
  • 73
  • An array-index expression is equivalent to one written as a pointer and offset. cf. Kernighan & Ritchie p99. – AruniRC Apr 22 '10 at 01:24