Note, this question is specifically about an "array-of-arrays", not multi-dimensional array, not jagged array, but a fixed size, square array. In C++ this would be a "plain old array", but this question is not about interoperation, it is just plain C# that gives a "Stack overflow", indicating that the array passed ends up being empty in the receiving function.
Constructing a plain array seems to work fine:
int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 5, 6, 7, 8};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };
int[][] lists = new int[][] {
list1, list2, list3, list4
};
Calling the function in question seems to all be safe, normal C# code:
Solution solution();
Console.WriteLine(a.solution(lists));
Now, the size of the arrays is not included in the information, of course. Which is precisely the problem. But what part of this C# is "unsafe" ? Didn't we have a keyword for that?
Function signature int int[][] f()
, in a seperate class:
public int solution(int[][] A){
// 1) try to access A using A.Length, which doesn't turn out to have the right information
// 2) get a segfault
// 3) cry
return A.Length;
}
The question is: "How do you pass an array-of-arrays into a C# function, such that the signature of Solution.solution doesn't need to change?"
I'm also quite confused that my code, which most definitely checks against A.Length and A[0].Length of the input, still segfaults. I would have thought that C# code that is not marked "unsafe" explicitly, would therefore be "safe". But I suppose that "safe" doesn't mean that arrays passed in the manner above actually arrive at the called function.
The goal, in the end, is to be able to do Codility tests locally, with however many testcases I want.
Note: the full source code, just two small files, with a little bit of irrelevant code (inside Solution class), but with a Makefile and everything you need, is right here: https://github.com/TamaHobbit/ActualSolution/blob/master/test_framework.cs
The full text of the error message:
Stack overflow: IP: 0x41ed8564, fault addr: 0x7ffc3c548fe0
Stacktrace:
at Solution.DFS (int,int,int) [0x00040] in <6d4ef1577c8c4e11a148ddbe545112a9>:0
<...>
at Solution.solution (int[][]) [0x00022] in <6d4ef1577c8c4e11a148ddbe545112a9>:0
at TestFramework.Main () [0x00068] in <6d4ef1577c8c4e11a148ddbe545112a9>:0
at (wrapper runtime-invoke) object.runtime_invoke_void (object,intptr,intptr,intptr) [0x0004c] in <a07d6bf484a54da2861691df910339b1>:0
You have an array of arrays. Therefore, at index 0, 1, 2, and 3, you have an array of 4 items. Specifically you have an array of 4 arrays. Here is how to get the size of them:
public static void Main()
{
int[] list1 = new int[4] { 1, 2, 3, 4 };
int[] list2 = new int[4] { 5, 6, 7, 8 };
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };
int[][] lists = new int[][] { list1, list2, list3, list4 };
var size = GetSize(lists);
}
public static int GetSize(int[][] items)
{
var arrAt0 = items[0].Length;
var arrAt1 = items[1].Length;
// Etc...
return items.Length;
}
To get an item out of the array, you will do it like this:
var lastItemInArray1 = lists[0][3];