0

Arrays can be linear (one dimension) or multi-dimensional. What is the difference between these arrays as long as they give the same result?

I think an array is a set of consecutive addresses of the same type and size in memory. Is this applicable to multi-dimensional array?

#include <iostream>
using namespace std;


int main()
{

    int array1[4]       = {0, 1, 2, 3};
    int array2[1][4]    = {0, 1, 2, 3};
    int array3[1][2][2] = {0, 1, 2, 3};

    cout << array1[0]       << endl; // 0
    cout << array2[0][0]    << endl; // 0
    cout << array3[0][0][0] << endl; // 0

    cout << array1[2]       << endl; // 2
    cout << array2[0][2]    << endl; // 2
    cout << array3[0][1][0] << endl; // 2

    return 0;
}
Melebius
  • 6,183
  • 4
  • 39
  • 52
Lee_Wong
  • 103
  • 1
  • 11
  • 1
    the difference is the way the elements are accessed. If you need a 3d structure you dont want to write `array3[ i + size_i * j + size_i * size_j * k]` but you want to write `array3[i][j][k]` – 463035818_is_not_an_ai Dec 13 '16 at 11:50
  • @tobi303 That's perhaps a little disingenuous because of potential benefits from spacial locality. – erip Dec 13 '16 at 11:52
  • 1
    @erip not sure if I understand your comment..... – 463035818_is_not_an_ai Dec 13 '16 at 11:53
  • 2
    A 2D array, is an array of arrays. It's elements are consecutive arrays. A 3D array, is an array of 2D arrays. It's elements are consecutive 2D arrays. And so forth. So I believe your intuition is correct. You should note that initializing all the arrays the same, is syntactic sugar. – StoryTeller - Unslander Monica Dec 13 '16 at 11:55

3 Answers3

1

Multidimensional arrays can be represented one of at least two ways: a single 1x(N*M) array, or as N number of 1xM arrays (which are consecutive in memory, as well). To fit with your nomenclature, we'll call the former a linear array and the latter a "multidimensional" array.

Languages which are row order (like C++) benefit greatly from cache coherence (due to spacial locality) when arrays are stored linearly. When iterating over columns, you'll find your cache misses much more frequently.

This is obviously a function of the size of your caches and how often you think you'll be iterating over the matrices, so make sure it's a bottleneck before making huge design decisions.

For more information, see this question and wikipedia.

Community
  • 1
  • 1
erip
  • 16,374
  • 11
  • 66
  • 121
1

It is just syntactic sugar. You could declare a plain int as int x; or as int x[1][1][1][1][1];. It won't affect memory layout, but the latter form is very inconvenient to use, when all you need is a single integer.

Similarly, when working with data that should represent 2 dimensions, it is inconvenient to work with a single "mangled" array that you have to access through [i*YSIZE + j] rather than [i][j].

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

Say I have to store 10 1-dimensional arrays together. So what will you do?

Merge them into one large array?

That is one possible solution! But Hell no! What's more neat and readable solution? Make an array of those one dimensional array! Isn't it? That gives us a 2 dimensional array.

So that's what the importance of a multidimensional array even though you can access values in all types of arrays.

I hope you haven't started coding a lot now. Cause once you start coding the use of single dimension and multi dimension become more obvious rather than gathering knowledge from reading.

bharadhwaj
  • 2,059
  • 22
  • 35