How are 3D arrays stored in C?

前端 未结 8 2189
半阙折子戏
半阙折子戏 2020-11-29 23:17

I understand that arrays in C are allocated in row-major order. Therefore, for a 2 x 3 array:

0  1
2  3
4  5

Is stored in memory as

相关标签:
8条回答
  • 2020-11-29 23:45

    Let's say you have an array char arr[3][4][5]. It is an array of 3 arrays of 4 arrays of 5 chars.

    For simplicity, let's say that the value in arr[x][y][z] is xyz and in arr[1][2][3] we store 123.

    So the layout in memory is:

      |  00  01  02  03  04  05  06  07  08  09  10  11  12  13  14  15  16  17  18  19
    --+--------------------------------------------------------------------------------   
    00| 000 001 002 003 004 010 011 012 013 014 020 021 022 023 024 030 031 032 033 034 
    20| 100 101 102 103 104 110 111 112 113 114 120 121 122 123 124 130 131 132 133 134 
    40| 200 201 202 203 204 210 211 212 213 214 220 221 222 223 224 230 231 232 233 234
    

    arr[0], arr[1] and arr[2] are coming one after another, but each element in the is of type char[4][5] (those are the three rows in the table).

    arr[x][0] - arr[x][3] are also coming one after another, and each element in them is of type char[5] (those are the four parts of each line in the table, 000 - 004 is one element of arr[0][0] )

    arr[x][y][0] - arr[x][y][4] are 5 bytes that are coming one after another.

    0 讨论(0)
  • To answer OP's comment to the main question (it will be somewhat long, so I decided to go with an answer, not a comment):

    Should arrays in C be declared as array[ny][nx] where ny and nx are the number of elements in the y and x direction. Furthermore, does that mean that my 3D array should be declared as array[nz][ny][nx]?

    In math, a MxN matrix has M rows and N columns. A usual notation for a matrix element is a(i,j), 1<=i<=M, 1<=j<=N. So the first matrix in your question is a 3x2 matrix.

    Indeed it is different from the notation typically used for e.g. GUI elements. A 800x600 bitmap has 800 pixels horizontally (along X axis) and 600 pixels vertically (along Y axis). If some would want to describe it as a matrix, in math notation it would be a 600x800 matrix (600 rows, 800 columns).

    Now, multidimensional arrays in C are stored in memory in such a way that a[i][j+1] is next to a[i][j] while a[i+1][j] is N elements away. It is usually said that "the last subscript varies fastest", or often as "stored by rows": a row (i.e. elements with same first index) in a 2-dimensional matrix has placed contiguously in memory while a column (same second index) consist of elements lying far away from each other. It is important to know for performance considerations: access to neighbor elements is usually much faster (due to HW caches etc.), so for example nested loops should be organized such that the innermost one iterates over the last index.

    Back to the question: if your mental picture (abstraction) of a 2D array is that of a lattice in Carthesian coordinates, then yes, you may think of it as array[NY][NX] in C. However if you need to describe real 2D or 3D data as an array, the choice of indexes probably depends on other things: data formats, convenient notation, performance, etc. For example, if the in-memory representation for a bitmap is array[NX][NY] in a format you need to work with, you will declare it that way, and maybe you don't even need to know that the bitmap becomes sort of "transposed" :)

    0 讨论(0)
提交回复
热议问题