void pointer to vector elements - c++

I want to have a void pointer to a vector.
void *para;
vector<double> x(2);
x[0] = 0;
x[1] = 1;
para = &x;
I can now use the vector like this.
vector<double> k = *(static_cast<vector<double>*>(para));
cout << k[0] << "\n";
Now I want to access the elements of the vectors through the pointer. How can I do that? But now I want to get the elements of the vector x, directly via the void pointer para, without using the new vector k. Something like this:
double k = ??? // here should be the element of x via para
Thank's in advance.

Now I want to access the elements of the vectors through the pointer.
This has two steps: reinterpret the pointer as a vector, then access elements.
How can I do that? But now I want to get the elements of the vector x, directly via the void pointer para, without using the new vector k.
void *para;
vector<double> x(2);
para = &x;
// step 1: reinterpret the pointer as a vector
auto *voidToVector = reinterpret_cast< vector<double>* >(para);
// step 2: access elements
double k = (*voidToVector)[0];
That said, please DON'T store data in your application as void*. Every time you do, you can assume that a developer will probably see it later and die a bit inside (and that developer may even be you).
Edit:
For the calculation in the function, I need to access the elements of the vector, vector>>> accel; Is there a better way????
Consider this:
class FourDimensionalVector
{
public:
FourDimensionalVector(std::size_t x, std::size_t y, std::size_t z, std::size_t a)
: d1{x}, d2{y}, d3{z}, d4{a}
, data{ d1 * d2 * d3 * d4 }
{
}
// ALL access to the vector elements can/should be done through this
double& operator()(std::size_t x, std::size_t y, std::size_t z, std::size_t a)
{
assert(d1 > x); // same for the other dimensions
return data[x * d1 + y * d2 + z * d3 + a];
}
// implement other interface elements here (iteration access, reading the size,
// resetting the values, etc.
private:
std::size_t d1, d2, d3, d4;
std::vector<double> data; // store flattened data
};
With this, you just pass a reference to the data as a parameter, and edit it as needed. I'm not sure the indexing logic is OK (and the code is incomplete), but the idea is the same.
Client code:
void YourFunction(FourDimensionalVector& fdv)
{
fdv[2, 3, 4, 0] = fdv[2, 3, 4, 0] + 0.38;
}
This solution is strongly typed, efficient and clean (it avoids the casting completely and the void*).
[edit by Jojia]: But what is the difference between your solution and this one
vector<vector<vector<vector<double>>>> Xaccel;
void initialize(&Xaccel); //This function initializes values for all elements of Xaccel
void myfunction(&Xaccel){
double x = Xaccel[0][0][0][0];
}
I don't see any difference to your solution. BUT: I thought passing these large objects to the function myfunction might be a problem. Is that correct?
[edit by utnapistim]:
From a semantical point of view, they are the same: both send the same data to the function and allocate the same memory (my solution also stores dimensions, but whatever).
From a maintenance/reusability/testability/modularity point of view, they are quite different: my solution abstracts away the fact that your four dimensional matrix is a vector (of vector of vector ... ).
This will allow you to write your client code using a 4d matrix interface (which you can implement to match the needs of your client code) instead of a vector, and allow you to define your operations in terms of a matrix.
For example, the vector<vector<vector<double>>> code can be used to create a sparse/asymetrical matrix (with the first line in the outer vector being three times longer than the second line and so on). The matrix class would prevent that by cutting access to the vector.
If you choose to keep the vector<vector...> solution, at least typedef it:
typedef vector<vector<vector<double>>> FourDMatrix;
FourDMatrix Xaccel;
void initialize(FourDMatrix& Xaccel);
void myfunction(FourDMatrix& Xaccel);

In this line you create a copy:
vector<double> k = *(static_cast<vector<double>*>(para));
So one of the solution would be to use reference instead:
vector<double> &k = *(static_cast<vector<double>*>(para));
k[0] = 123;
Or pointer
vector<double> *pk = static_cast<vector<double>*>(para);
(*pk)[0] = 123; // but usage is more explicit, so reference would be better
I assume you know what you are doing and you really need to pass vector through a void *. One of the reasons could be passing pointer to vector as a cookie to C API that expects void *. Note there is very low chance to have a good reason to use void * in c++ itself, you should use boost::any or boost::variant for type safety.

Related

Passing pointer of multi-dimensional pointer array to a function

ok so suppose I have a function myFunction. Then in main i have a multi dimensional array of pointers. I want to pass a pointer to this array of pointers into myFunction. How would I do that? I know that If you want to pass an int to my function, one can write the function as
myfunct( int x) { ...}
What would that type of x be if I have to pass a pointer to an array of pointers? Thanks in advance :D
Typically you want to modify the elements of an array rather then the actual pointer. The actual pointer is given by malloc and if you change it, by writing directly to the value, it won't affect the memory allocation (except you might loose the initial pointer...).
This might be what you're looking for in a 2D array.
void myfunct(int** ptr,int items, int array_items)
{
//some code
}
int main(int argc, char *argv[])
{
const auto items = 5;
const auto array_items = 7;
int** multi_dimensional_array = reinterpret_cast<int**>(std::malloc(items * sizeof(int*)));
for (auto i = 0 ;i < items;++i)
{
multi_dimensional_array[i] = static_cast<int*>(std::malloc(sizeof(int) * array_items));
}
myfunct(multi_dimensional_array,items,array_items);
//deallocate
}
Wrap your multidimensional array inside a class. That way you can carry the data and dimensions in one block and passing it around is as simple as moving around any other class.
Remember to observe the Rules of Three, Five, and Zero, whichever best applies to how you store your array inside your class. std::vector is a personal favourite because it allows you to use the Rule of Zero.
For example:
#include <iostream>
#include <vector>
struct unspecified
{
};
template<class TYPE>
class TwoDee{
int rows;
int cols;
std::vector<TYPE> data;
public:
TwoDee(int row, int col):rows(row), cols(col), data(rows*cols)
{
// does nothing. All of the heavy lifting was in the initializer
}
// std::vector eliminates the need for destructor, assignment operators, and copy
//and move constructors. All hail the Rule of Zero!
//add a convenience method for easy access to the vector
TYPE & operator()(size_t row, size_t col)
{
return data[row*cols+col];
}
TYPE operator()(size_t row, size_t col) const
{
return data[row*cols+col];
}
};
void function(TwoDee<unspecified *> & matrix)
{
// does stuff to matrix
}
int main()
{
TwoDee<unspecified *> test(10,10);
function(test);
}
To directly answer your question, typically the type passed will be int * for a vector of int, and int ** for a 2D array of int
void myfunct( int **x)
{
x[2][1] = 25;
return;
}
If for some reason you wanted that to be an array of int pointers instead of int you need an extra *.
void myfunct( int ***x)
{
*(x[2][1]) = 25;
return;
}
Let me first try to interpret the exact type that you want to deal with. I suppose in your main function there is a "multidimensional array" which stores pointers for each element. As an example, let's say you have a 3-dimensional array of pointer to integer type.
Assume that you know the size of the array:
C style array will look like this:
int *a[4][3][2];
that means a is a 4x3x2 array, and each element in the array is a pointer to integer. So overall you now have 24 pointers to integer in total, as can be seen by testing the result of sizeof(a) / sizeof(int*) (the result should be 24). Okay, so far so good. But now I guess what you want is a pointer to the array a mentioned above, say b, so b is defined
int *(*b)[4][3][2] = &a;
Notice that although now b looks intimidating, in the end it is just a pointer which just stores an address, and sizeof(b) / sizeof(int*) gives 1 as the result. (The * inside parenthesis indicates b is pointer type, so b is a pointer to a "multidimensional array" of pointers to integer.)
Now to pass b to myFunction, just give the same type of b as argument type in the declaration:
void myFunction(int *(*x)[4][3][2]) {
// do something
}
And that's it! You can directly use myFunction(b) to invoke this function. Also, you can test that inside myFunction, x is still of the size of one pointer, and *x is of the size of 24 pointers.
*Note that since we are passing a pointer to array type into the function, the array-to-pointer decay does not apply here.
Assume you don't know the size of the array at compile time:
Say you have int N1 = 4, N2 = 3, N3 = 2; and you want to initialize a N1xN2xN3 array of pointer to integer, you cannot directly do that on the stack.
You could initialize use new or malloc as suggested in #Mikhail's answer, but that approach takes nested loops for multidimensional arrays and you need to do nested loops again when freeing the memory. So as #user4581301 suggests, std::vector provides a good wrapper for dynamic size array, which do not need us to free the memory by ourselves. Yeah!
The desired array a can be written this way (still looks kind of ugly, but without explicit loops and bother of freeing memory)
std::vector<std::vector<std::vector<int*>>> a (N1,
std::vector<std::vector<int*>> (N2,
std::vector<int*> (N3)
)
);
Now, b (the pointer to a) can be written as
auto *b = &a;
You can now pass b with
void myFunction(std::vector<std::vector<std::vector<int*>>>* x) {
// do something
}
Notice that the * before x means x is a pointer.

Moving from old C-style pointer to C++ smart pointers with little changes in the code?

I have a function in which the nodes of a binary 'tree' are populated with values recursively computed based on the input vector, which represents the values on the leaves. An old C++ implementation of the function is as follows
using namespace std;
double f(const size_t h, vector<double>& input) {
double** x;
x = new double*[h+1];
x[0] = input.data();
for (size_t l = 1; l <= h; l++)
x[l] = new double[1<<(h-l)];
// do the computations on the tree where x[l][n] is the value
// on the node n at level l.
result = x[l][0];
for (size_t l = 1; l <= h; l++)
delete[] x[l];
delete[] x;
return result;
}
Now I'm trying to write a 'modern' implementation of the code using smart pointers in C++11/C++14. I attempted to define x using std::unique_ptr specialization for arrays so that I do not have to change the 'computation' procedure. The obvious problem with such an approach is that the contents of `input' will be deleted at the end of the function (because the unique pointer that takes the ownership of the data will be destroyed at the end of the function).
One simple (and perhaps safe) solution would be to allocate the memory for the whole tree (including the leaves) in x and copy the values of the leaves from input to x[0] in the beginning of the function (in this case I can even used nested std::vectors instead of std::unique_ptrs specialized for arrays as the type of x). But I prefer to avoid the cost of copying.
Alternatively one can change the computational procedures to read the values of the leaves directly from input not from x which requires changing too many small pieces of the code.
Is there any other way to do this?
C++11/14 didn't really introduce anything that wasn't already achievable prior using the modern std::vector for managing the memory of dynamic arrays.
The obvious problem with [std::unique_ptr] is that the contents of `input' will be deleted at the end of the function
Indeed. You may not "steal" the buffer of the input vector (except into another vector, by swapping or moving). This would lead to undefined behaviour.
Alternatively one can change the computational procedures to read the values of the leaves directly from input not from x which requires changing too many small pieces of the code.
This alternative makes a lot of sense. It is unclear why the input vector must be pointed by x[0]. The loops start from 1, so it appears to not be used by them. If it is only ever referenced directly, then it would make much more sense to use the input argument itself. With the shown code, I expect that this would simplify your function greatly.
Also the fact that the input is not taken as const std::vector& bothers me.
This is another reason to not point to the input vector from the modifiable x[0]. The limitation can however be worked around using const_cast. This is the kind of situation what const_cast is for.
Let us assume henceforth that it makes sense for the input to be part of the local array of arrays.
One simple (and perhaps safe) solution would be to allocate the memory for the whole tree (including the leaves) in x ... I can even used nested std::vectors ... But I prefer to avoid the cost of copying.
You don't necessarily need to copy if you use a vector of vectors. You can swap or move the input vector into x[0]. Once the processing is complete, you can restore the input if so desired by swapping or moving back. None of this is necessary if you keep the input separate as suggested.
I suggest another approach. The following suggestion is primarily a performance optimization, since it reduces the number of allocations to 2. As a bonus, it just so happens to also easily fit with your desire to point to input vector from the local array of arrays. The idea is to allocate all of the tree in one flat vector, and allocate another vector for bare pointers into the content vector.
Here is an example that uses the input vector as x[0], but it is easy to change if you choose to use input directly.
double f(const size_t h, const std::vector<double>& input) {
std::vector<double*> x(h + 1);
x[0] = const_cast<double*>(input.data()); // take extra care not to modify x[0]
// (1 << 0) + (1 << 1) + ... + (1 << (h-1)) == (1 << h) - 1
std::vector<double> tree((1 << h) - 1);
for (std::size_t index = 0, l = 1; l <= h; l++) {
x[l] = &tree[index];
index += (1 << (h - l));
}
// do the computations on the tree where x[l][n] is the value
// on the node n at level l.
return x[l][0];
}
This certainly looks like a job for a std::vector<std::vector<double>>, not std::unique_ptr, but with the additional complexity that you conceptually want the vector to own only a part of its contents, while the first element is a non-owned reference to the input vector (and not a copy).
That's not directly possible, but you can add an additional layer of indirection to achieve the desired effect. If I understand your problem correctly, you want to behave x such that it supports an operator[] where an argument of 0 refers to input, whereas arguments > 0 refer to data owned by x itself.
I'd write a simple container implemented in terms of std::vector for that. Here is a toy example; I've called the container SpecialVector:
#include <vector>
double f(const std::size_t h, std::vector<double>& input) {
struct SpecialVector {
SpecialVector(std::vector<double>& input) :
owned(),
input(input)
{}
std::vector<std::vector<double>> owned;
std::vector<double>& input;
std::vector<double>& operator[](int index) {
if (index == 0) {
return input;
} else {
return owned[index - 1];
}
}
void add(int size) {
owned.emplace_back(size);
}
};
SpecialVector x(input);
for (std::size_t l = 1; l <= h; l++)
x.add(1<<(h-l));
// do the computations on the tree where x[l][n] is the value
// on the node n at level l.
auto result = x[1][0];
return result;
}
int main() {
std::vector<double> input { 1.0, 2.0, 3.0 };
f(10, input);
}
This approach allows the rest of the legacy code to continue to use [] exactly as it did before.
Write a class Row, which contains a flag for ownership controlling destruction behavior and implement operator[], then create a vector of row.
As noted above, you have issues if input is constant, as you cannot explicitly enforce it at compiler level, and you have to be careful not to write where you cannot, but this is not worse then what you have now.
I have not tried to compile it, but your new Row class could look a bit like this.
class Row
{
double *p;
bool f;
public:
Row() :p(0), f(false) {}
void init(size_t n) { p = new double[n]; f=true; }
void init(double *d) { p=d;, f=false;}
double operator[](size_t i) { return p[i]; }
~Row() { if (flag) delete[] p; }
};

How to access a dynamically allocated matrix in c++?

I have created a dynamic matrix of class objects but i have made a big mess with handling the returned pointers.
My intention is to create a matrix of class Point( Int x,Int y) and later to use it in different ways in the program.
Everything is working but i can't figure out the returned pointers game between the functions.
class Point
{
private:
int x;
int y;
public:
Point(int x,int y);
void SetPoint(int x,int y);
};
In a second class I use a Point object as class member.
Init_Pallet() is used to Initialize the Matrix.
class Warehouse
{
private:
Robot r1,r2;
Point *Pallet_Matrix;
public:
Point* Init_Pallet();
};
This is the Init function
Point* Warehouse::Init_Pallet()
{
int rows =10,cols =10;
Point** Pallet_Matrix = new Point*[rows];
for (int i = 0; i < rows; i++)
Pallet_Matrix[i] = new Point[cols];
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; j++) //Pallet matrix Init, x point for robots amount in position and y for box amount
Pallet_Matrix[i][j].SetPoint(0,0);
return *Pallet_Matrix;
}
The Init function is called by WareHouse C'Tor (ignore the other vars)
Warehouse::Warehouse(Robot p_r1,Robot p_r2): r1(p_r1),r2(p_r2)
{
this->r1=p_r1;
this->r2=p_r2;
Point *p =Init_Pallet();
this->Pallet_Matrix=p;
}
My question is: How do I return the address to the beginning of the matrix from the Init function to the C'Tor who called it?
And second question: how do i access the matrix different locations in the format of Matrix[i][j] after returning the matrix adress to the C'Tor.
Thank you in advance for all the help and your time.
You should just have Init_Pallet return a Point** and then do return Pallet_Matrix;. Currently you're copying one of the Point*s that you allocated out of the function, so the copy is no longer part of a contiguous array that you can index.
Don't forget to delete[] the dynamically arrays in your destructor.
However, you should much prefer to use the standard library containers like std::array or std::vector. Then you don't need to worry about the dynamic allocation yourself and no pointers to get in a mess with.
If I were doing it, I would just have:
class Warehouse
{
public:
Warehouse() : Pallet_Matrix() { }
private:
Robot r1,r2;
std::array<std::array<Point, 10>, 10> Pallet_Matrix;
};
And that's it. No init needed. No dynamic allocation. No assigning 0 to every element (if you give Point a default constructor that zero-initialises). Done.
How do I return the address to the beginning of the matrix from the Init function to the C'Tor?
In case you would really need just an address of first element, pretty straightforward would be:
return &Pallet_Matrix[0][0];
how do i access the matrix different locations in the format of Matrix[i][j] after returning the matrix address
Init_Pallet is a member function, which could simply work with the Pallet_Matrix member directly. Otherwise, the Init_Pallet function could actually return Point**, which should however make you feel that something's wrong with this code.
Better[1] solution would be:
Define the default constructor for Point:
class Point
{
public:
Point() : x(0), y(0){}
...
Use std::vectors instead of dynamically allocated arrays:
class Warehouse
{
private:
std::vector< std::vector<Point> > Pallet_Matrix;
and instead of:
Point *p =Init_Pallet();
this->Pallet_Matrix=p;
you would simply use std::vector's constructor:
int rows = 10, cols = 10;
Pallet_Matrix = std::vector< std::vector<Point> >(rows, cols);
[1] Better = You don't want to handle the memory management on your own.
The problem is that the returned type of Init_Pallet() is wrong — its a row, not a matrix. And in the last line of Warehouse::Init_Pallet() you dereference the proper pointer to matrix obtaining the pointer to the first row of the matrix.
You need to write Point **Pallet_Matrix; in Warehouse, use Point** Warehouse::Init_Pallet() definition of Init_pallet(), and return Pallet_Matrix in the last line of Init_Pallet().
The notation Point *row means the row is "the array of points" or "the pointer to the beginning of the array of points". The notation Point **matrix means the matrix is "the array of pointers to the beginnings of the arrays of points" or "the pointer to the beginning of such an array".
First: are the dimensions really constant, or is this just an
artifact of your having simplified the code for posting? If
they are really constant, there's no need for dynamic
allocation: you can just write:
Point palletMatrix[10][10];
and be done with it. (If you have C++11, it's even better; you
can use std::array, and palletMatrix will have full object
semantics.)
If you do need dynamic indexes, the only reasonable way of
doing this is to write a simple matrix class, and use it:
class Matrix
{
int m_rows;
int m_columns;
std::vector<Point> m_data;
public:
Matrix( int rows, int columns )
: m_rows( rows )
, m_columns( columns )
, m_data( rows * columns, Point( 0, 0 ) )
{
}
Point& operator()( int i, int j )
{
return m_data[ i * m_columns + j ];
}
// ...
};
Trying to maintain a table of pointers to tables is not a good
solution: it's overly complex, it requires special handling to
ensure that each row has the same number of columns, and it
generally has poor performance (on modern machines, at least,
where locality is important and multiplication is cheap).
Note too that the actual data is in an std::vector. There are
practically no cases where a new[] is a good solution; if you
didn't have std::vector (and there was such a time), you'd
start by implementing it, or something similar. (And
std::vector does not use new[] either.)
EDIT:
One other thing: if you're putting Point in a matrix, you might
want to give it a default constructor; this often makes the code
simpler.

Striding across members of a vector of structures

Is there an easy way to stride through an STL vector of structures by member? In other words, if I have a struct like this:
struct foo {
double x, y, z;
};
in a vector std::vector<foo> bar(20), can I stride across the array picking out x from each struct?
I've tried this, but it does not seem to work:
for (int i=0; i<20; ++i)
{
double xx = (&bar[0].x + i*sizeof(bar[0]))->x;
}
Why doesn't that work? Does sizeof(bar[0]) not account for the padding between structs?
Note: I realize this is a really silly way to access x in a loop, but this loop is just an experiment to see if the stride works or not.
If it helps, I want to do this so I can pass bar to a library routine that accepts a pointer and a stride as constructor parameters to its own internal datatype. I could, of course, convert my code from AoS to SoA, but I don't want to do that unless I absolutely have to.
I think I'd compute the stride directly instead, using something like:
struct point {
double x, y, z;
};
int main() {
point points[2];
std::cout << "stride = " << (char *)(&(points[1].x)) - (char *)(&(points[0].x)) << "\n";
}
&bar[0].x is a pointer to double. You're adding i*sizeof(bar[0]) to it.
The effect is that the address stored in the pointer increases by i*sizeof(bar[0])*sizeof(double) which is not what you expect.
A correct expression is
&bar[0].x + i*sizeof(bar[0])/sizeof(double)
&bar[0].x is a pointer to double. To add the appropriate shift, you would need to cast it into char*, then double* again.
double xx = *reinterpret_cast<double*>(reinterpret_cast<char*>(&bar[0].x) + i*sizeof(bar[0]));
In anycase, you should really consider using standard algorithms for your purpose.

passing a multidimensional array to a function with argument of type double *

I am using a library that takes a pointer to a double array as an argument, called as follows:
const int N=10;
const int J=20;
double my_array[N*J];
libFunc(my_array, N, J);
I would prefer to work with multidimensional arrays in my code, and I have discovered that I can call libFunc by dereferencing my multidimensional double array as follows
double my_array2[N][J];
libFunc(&my_array2[0][0], N, J);
However, I am worried that this code might not be portable, that it may not continue to work as N and M get large, and that there may be other hidden problems.
Why is this bad, what should I look out for here? What is the proper way to use multidimensional arrays and pass them to libFunc as if they were ordinary double arrays?
Edit: Read the comments below the selected answer for a discussion of the issue at hand. It seems that if I declare a static array, as is done above, then this code should work on most compilers. However if the array is dynamically allocated there may be an issue.
There is no simple way short of making a copy. Accessing an array outside its bounds is undefined behaviour, and you won't get around this.
Now, it is possible in many situations that your code works, simply because the memory for T[M * N] and for T[M][N] is laid out in the same way. As long as the caller and the callee aren't visible to the compiler at the same time, I would hazard a guess that this should work.
But imagine this scenario:
T a[M][N];
for (size_t i = 0; i != M * N; ++i)
{
a[0][i] = 0;
}
Here the compiler may reason that a[0][N] is out of bounds, and thus there is undefined behaviour, and the compiler may legally omit the entire loop, or make the application crash or wipe your hard disk.
So... take your pick. Undefined behaviour is around somewhere, but you might get lucky.
You are basically screwed: The function expects a double *, so you should give it a double *.
The easiest and safer way to do that would be to use a wrapper. Something like:
template<size_t M, size_t N>
class Double2DArray
{
std::vector<double> m_container ; // It could be a double[M * N]
// But it could explode your stack
// So let's use the heap
public :
// Etc.
Double2DArray()
: m_container(M * N)
{
// I assume the default you want is a vector already
// filled with 0.0
}
size_t getSizeM() { return M ; }
size_t getSizeN() { return N ; }
double & operator() (size_t p_m, size_t p_n)
{
return m_container[p_m * N + p_n] ;
}
double * data()
{
return &(m_container[0]) ;
}
// etc.
} ;
Of course, this code is not complete: At the very least, you should add the const versions of the accessors, probably handle copy-construction and assignment, etc.. I don't know your exact needs, so, your mileage may vary, but the core idea is there...
You could use this wrapper as follow:
void foo()
{
Double2DArray<42, 13> my2DArray ;
// etc.
my2DArray(3, 5) = 3.1415 ; // set some value
double d = my2DArray(13, 2) ; // get some value
// etc.
libFunc(my2DArray.data(), my2DArray.getSizeM(), my2DArray.getSizeN());
}
I would even overload libFunc to be safer:
template<size_t M, size_t N>
inline void libFunc(Double2DArray<M, N> & p_my2DArray)
{
libFunc(p_my2DArray.data(), M, N);
}
This way I could be able to call it without needed to give it again and again the size of the array (it's so easy to mix M and N):
void foo()
{
Double2DArray<42, 13> my2DArray ;
// etc.
libFunc(my2DArray);
}
This is how I would use multidimensional arrays and feed it to a C-like API expected a contiguous array of doubles.
P.S.: If M and N are not know at compile time, you only need to remove the template, and make the M and N parameters of the constructor, and everything works (almost) the same.
C++ uses row-major ordering so your multidimensional array is in fact a continuous 1-dimensional region in memory.
Even if declared for example 2-dimensional, it's accessed via index = x + y * height, so there should be no portability concerns...
The C++ documentation tells:
Multidimensional arrays are just an abstraction for programmers, since
we can obtain the same results with a simple array just by putting a
factor between its indices
(Here's also an explaination for visual c++)