How is an n-dimensional (n> = 2) array represented in memory?

Can someone provide me with a formula so that I can understand the memory representation of an n-dimensional (n> = 2) array like "How_are_two-dimensional_arrays_represented_in_memory" ?

This calculation is applicable only for 2D arrays.

How to calculate, say, a 5D array?

Ok ....

I think I found the answer: Array_data_structure # Two-dimensional_arrays

+4
source share
3 answers

A 2-dimensional array in C is no larger or smaller than an array of arrays. A 3-dimensional array is an array of arrays of arrays. And so on.

The corresponding section from C99 standard is 6.5.2.1, “Array Subscribers”:

Operators followed by an index denote a multidimensional array element. If E is an n-dimensional array (n ≥ 2) with dimensions i × j ×., × k, then E (used as except for lvalue) is converted to a pointer to an (n - 1) -dimensional array with sizes j ×., × k. If the unary * operator is applied to this pointer explicitly or implicitly as a result of a subscription, the result is a directional (n - 1) -dimensional array, which itself is converted to a pointer if used as other than lvalue. It follows that the arrays are stored in lowercase order (the last index changes faster).

Some confusion is caused by the fact that the index operator is defined in terms of pointer arithmetic. This does not mean that arrays "really point" - and in fact they are very definitely not. Declaring an array object does not create any pointer objects at all (unless, of course, it is not an array of pointers). But an expression that refers to an array usually (but not always) "decomposes" into a pointer to the first element of the array (the value of the pointer, not the pointer object).

Now simple array objects, no matter how they are measured, are quite inflexible. Prior to C99, all array objects should have a fixed size defined at compile time. C99 introduced variable-length arrays (VLAs), but even so, the size of the VLAs is fixed when it is declared (and not all compilers support VLAs, even 12 years after the release of the C99 standard).

If you need something more flexible, the general approach is to declare a pointer to the element type, then select the array with malloc() and point to the first element of the array:

 int *ptr = malloc(N * sizeof *ptr); if (ptr == NULL) /* handle allocation failure */ 

This allows you to refer to the elements of the array allocated by the heap using the same syntax that you would use for the declared array object of a fixed size, but in arr[i] expression arr splits to a pointer, whereas in ptr[i] `ptr is already pointer.

The same can be extended to higher dimensions. You can select an array of pointers, and then initialize each pointer to point to the beginning of the selected array.

This gives you something that is very similar to a 2-dimensional (or more) array, but you need to manage the memory yourself; what's the price of more flexibility.

Strictly speaking, this is not a two-dimensional array. A 2-dimensional array, as I said above, is just an array of arrays. It is probably not completely unreasonable to think of it as a two-dimensional array, but this contradicts the use in the C standard; it looks like a link to a linked list as a 1-dimensional array.

comp.lang.c FAQ is a good resource; section 6, which covers arrays and pointers, is especially good.

+4
source

A 2-dimensional array is indeed an array of pointers to arrays. A 2-dimensional array of integers a[i][j] will take i*sizeof(int*) for the array of pointers and i*j*sizeof(int) for the final array.

3-dimensional array a[i1][i2][i3] is an array of pointers to arrays of pointers to arrays. The first level of arrays contains pointers i1 , the second level contains pointers i1*i2 , the third level contains i1*i2*i3 integers.

In general, an N-dimensional array with dimensions i1..iN will have N-1 levels of pointer arrays and 1 level of int arrays. Arrays at level N have the length iN , and at this level there are arrays product of i1..iN-1 .

So, a 5-D array:

 1 array, length i1, of pointers i1 arrays, length i2, of pointers i1*i2 arrays, length i3, of pointers i1*i2*i3 arrays, length i4, of pointers i1*i2*i3*i4 arrays, length i5, of ints 

Hope this helps (and I hope I get the indexes right).

This wikipedia link you pointed out refers to / another kind of multidimensional array /. By default, C multidimensional arrays are what I just described. You can also abstract them as a one-dimensional array. This saves memory and makes the entire array contiguous, but makes access elements a little more complex. For a 5-D example:

 // WARNING I AM CHANGING NOTATION. N1..N5 are the lengths in each direction. // i1..i5 are the indicies. int* bigarray = malloc(sizeof(int)*N1*N2*N3*N4*N5); // now instead of bigarray[i1][i2][i3][i4][i5], write this: *(bigarray + i1*N2*N3*N4*N5 + i2*N3*N4*N5 + i3*N4*N5 + i4*N5 + i5); 

each term has an offset times the number of elements that we need to compensate. For example, to increase the first dimension by one level, we need to go through the four remaining dimensions once to “wrap” if you do.

+2
source

How arrays are stored in memory for C, I, as I recall, are not standardized. But to get some information about arrays and how to store them in memory, see the following two links:

http://webster.cs.ucr.edu/AoA/Windows/HTML/Arraysa2.html

http://publications.gbdirect.co.uk/c_book/chapter5/arrays.html

The first link is more general and discusses various ways of storing arrays, while the second discusses the most likely way to store a C array in memory.

+1
source

Source: https://habr.com/ru/post/1380688/


All Articles