I've just been to an exam at local uni and they asked me to do basically a pathfinder. I got stuck, because I really wanted to pass a two-dimensional array to a function. I haven't come up with the idea of double pointer with heap allocation, but now that I think of it, it seems really ugly to not be able to pass it like
int function(int matrix[][]);
or maybe int function(int& matrix[][]);
something like that. What's the logic behind that restriction? Is there any way to do that on stack without malloc
?
EDIT
The array is dynamic, and I was restricted from using any wrappers, including those from standard library

- 3
- 3
-
5Pass a multi dimensional `std::array` or `std::vector`. Forget about C-style arrays or passing arrays as pointers+size - in C++ we have better options. And *don't* use `malloc` in a C++ program, please. – Jesper Juhl Mar 06 '19 at 17:13
-
1Possible duplicate of [Passing 2-D array as argument](https://stackoverflow.com/questions/12990568/passing-2-d-array-as-argument) – Richard Mar 06 '19 at 17:15
-
1The restriction is also the same in C, how do you know the dimensions of the array? So in C++, you use a vector or a matrix class, in C, you would use a `int**` and pass the width and height. – Matthieu Brucher Mar 06 '19 at 17:15
-
@Richard depends if the array size is static or not, which is not clear from OP's question. – Matthieu Brucher Mar 06 '19 at 17:16
-
Forgot to add, I was restricted from using anything but iostream, and that was the hassle. If I wasn't I wouldn't have been here – Anton Mar 06 '19 at 17:18
-
2I don't get why universities are still teaching severely obsolete C++98. Makes me sad ☹️ – Jesper Juhl Mar 06 '19 at 17:20
-
The proper duplicate then: https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new (if a C++ gold badge is around) – Matthieu Brucher Mar 06 '19 at 17:25
-
@MatthieuBrucher, I was asking for a stack way of doing stuff, heapifying everything will just add some boilerplate and asterisks in my case. – Anton Mar 06 '19 at 17:29
-
As far as I am aware the reason is that *native array types* in `C++` are *very fast* because the dimensions (all but one) must be specified. So that's why *array types* are restricted like that. If you want a *fully dynamic* (native) multidimensional array you need to implement it as an array of arrays. The syntax for that is obviously different. And it's slower. But you can make a just as fast one using *user defined types* (not allowed in your question). – Galik Mar 06 '19 at 17:30
-
Well, a 2D array can be generated on the stack as well through the same mechanisms. But if the sizes are dynamic, the matrix should not be on the stack. – Matthieu Brucher Mar 06 '19 at 17:32
-
@MatthieuBrucher, I think you misunderstood, what I meant by saying it was dynamic. I was given a task at a uni, so they definitely intended it to be using stack (who in the right mind would write a test with over 99999 pre-checked tiles). The array doesn't change its size in any way at any point. The only thing is that noone knows the final size of the array (I know it sound like it should use heap, but just trust me). – Anton Mar 06 '19 at 17:38
-
In that case, see @Richard's duplicate. – Matthieu Brucher Mar 06 '19 at 17:39
-
@MatthieuBrucher, still, that thread doesn't cover my question. Maybe I wasn't clear enough about it being dynamic in BOTH dimensions, and I can't just use a const at the second dimensions, since there isn't any. That's the problem – Anton Mar 06 '19 at 17:44
-
In that case, it's a dynamic 2D array, and you need a 1D indirection array, as explained in my duplicate. But you need FAR more information in your question to be able to say if it's one or the other. So definitely unclear what you are asking. – Matthieu Brucher Mar 06 '19 at 17:46
-
1@Galik `std::array` is just as fast as C-style arrays. And if you need dynamic arrays you have a *real* challenge if you want to beat `std::vector`. There really is *no* excuse to use C-Style arrays in C++, ever. – Jesper Juhl Mar 06 '19 at 17:59
-
@JesperJuhl The excuse here is that the OP was restricted from using user defined types. Otherwise you said pretty much what I said :) (also `std::array` and `std::vector` are not multidimensional - for that you have to define your own type) – Galik Mar 06 '19 at 18:06
1 Answers
pass a two-dimensional array to a function
First thing to realise is that a function argument cannot be an array. A typical way to pass arrays into a function is to pass a pointer to an element of the array.
In case of a 2D array, the elements of the array are 1D arrays. Here is an example:
int function(int matrix[][4]);
int main() {
int m[4][4];
function(m);
}
without
malloc
?
There's hardly ever a reason to use malloc
in C++.
what if the second dimension isn't const
Then it cannot be a multidimensional array. The type of an element of an array must be compile time constant. Since the size is part of the type of an array, this means that the inner dimension of a multidimensional array must be compile time constant. (Furthermore, the outermost dimension must also be compile time constant unless the array is allocated dynamically).
The trick to implement structures that are mathematically multidimensional is to represent them as a single dimensional array where each row of the inner dimension is after the previous row (this is how mutildimensional array types are implemented too; memory is one-dimensional). If the rows have the same width, then the multidimensional index can be converted into the flattened index using the formula:
flat_index = dim1_index + dim2_index * row_size

- 232,697
- 12
- 197
- 326
-
-
So, if I got you right, I have to use pointers to array of pointers as an argument (what I meant by saying double pointers, e.g. int** matrix)? – Anton Mar 06 '19 at 17:25
-
@Anton yes, see the duplicate I mentioned in my last comment. – Matthieu Brucher Mar 06 '19 at 17:26
-
@MatthieuBrucher This can take a pointer to an array of any size. But it can only take a pointer to an element of an array of 4 integers. – eerorika Mar 06 '19 at 17:27
-
Exactly, hence the proper duplicate. As you are a C++ gold badge, you can delete your wrong answer and close it as a proper duplicate. – Matthieu Brucher Mar 06 '19 at 17:27
-
So, last question is: do I use the argument int** matrix as matrix[i][j], or am I terribly mistaken? – Anton Mar 06 '19 at 17:31
-
@Anton No. You cannot use `int**` to point to an element of `matrix[N][M]`. – eerorika Mar 06 '19 at 17:32
-
-
-
Because it doesn't provide an answer to OP's question, and if it was about passing [][4], there are thousands of duplicates on the subject. And as a gold badge, you are supposed to identify duplicates and close them. – Matthieu Brucher Mar 06 '19 at 17:33
-
-
@MatthieuBrucher Existing duplicates don't make an answer wrong. Feel free to vote close if you find a duplicate. – eerorika Mar 06 '19 at 17:34
-
I have as unclear and then provided the duplicate. But I'm not a gold badge because I prefer quality over quantity. And the answer is wrong because doesn't answer OP's question. – Matthieu Brucher Mar 06 '19 at 17:35
-
@eerorika, but what if the second dimension isn't const, but instead some random number x defined at the start of the program? – Anton Mar 06 '19 at 17:40
-
-
@eerorika, I've seen it referenced somewhere as "*fake multidimensional array*". I think that's the way to go. Question answered. – Anton Mar 06 '19 at 17:52