In C when you declare e.g. int ar[x][y];
it actually is int[x*y]
, and you calls are compiled in needed manner.
But this array is stored on stack if made inside a function.
I'm looking for a way to have it stored on heap, while having the same functionality. Because when I was looking for a way to do is, the only thing I found was make int**
and then each of element (type int*
) would be pointer to another array. (Correct me if I'm wrong) It seems legit but the second way doesn't store the values one after another and also uses more memory. For ar[x][y]
first method is just x*y*sizeof(int)
, but for second it's x*y*sizeof(int)+x*sizeof(int*)
.
Summarizing is having two-dimensional array stored on heap possible, while maintaining functionality of int ar[x][y]
?
EDIT: I'm sorry for misleading everyone, but ar[3][4]
was just en example, I'm actually looking for a way to do this with non-constant size.
EDIT2: Actually I'm even looking for sth that will also work in raw C.