+ 12
How memory is allocated for multidimensional arrays in c and different languages?
if you know explain briefly
2 ответов
+ 5
In C, you allow a memory space of a certain size (which create a mono dimensional array). As for what you put as a value of your array, it is of your choice. If you put an array as the array's value, then it will create a two dimensional array, and you can continue like that recursively.
In C#, multi dimensional arrays ([,], [,,], ...) are in fact just one array and when you enter the indexes, the language translate them in a unique index which will be the corresponding for the uni dimensional array.
Other multi dimensional arrays ([][], [][][],...) are like in C.
In C++, it is the same as in C.
There are no arrays in python, all there is are lists.
I am not sure for other languages but I assume that it is the same but it is hidden.
0
In most programming languages there are no multidimensional arrays (C, C++, Java, Javascript, PHP. I don't know the others). However, arrays are supported that contain more arrays which is a way to "simulate" multidimensional arrays. This means that the array contains references or pointers to other arrays. These arrays can have different sizes, like
{
{1, 2, 5},
{3, 3},
{5, 2, 2, 4}
}
Another way to "simulate" a two-dimensional array is to store the values in a one-dimensional array and remember the width and height. Instead of
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
just write
{1, 2, 3, 4, 5, 6, 7, 8, 9}
To get the value at row 2 column 2, you can calculate the proper index in the one-dimensional array:
index = row * width + column
in this example:
index = 1 * 3 + 1 = 4; //remember to start counting at 0