-2

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
TamaMcGlinn
  • 2,840
  • 23
  • 34
  • 1
    The "function signature" you provided is not valid C#. – Dai Jun 18 '17 at 07:10
  • 3
    _"this question is specifically about an "array-of-arrays", not multi-dimensional array, not jagged array"_ -- uh, in C# an "array-of-arrays" is exactly what a "jagged array" is. Your question cannot be about the former, but at the same time _not_ be about the latter. It's not clear what you expect `Length` to be. An "array-of-arrays" is really just "an array". The `Length` of that object will tell you how many elements are stored in the array, i.e. how many inner arrays are contained in the outer array. Your question is very confusing...it seems like maybe you just don't understand arrays. – Peter Duniho Jun 18 '17 at 07:12
  • 2
    How do you `return A.Length;` from `void` method? – Guy Jun 18 '17 at 07:13
  • You should also provide the code causing the actual problem, i.e. the `1)` comment. – Guy Jun 18 '17 at 07:14
  • 2
    Without a good [mcve] it's impossible to understand your question. For sure, there's no such thing as "segfault" in C#. If you mean an `IndexOutOfRangeException` is being thrown, I can believe that. And the default, not-unsafe code could certainly throw that exception. Not-unsafe doesn't mean bug-free. It's up to _you_ to ensure the latter. – Peter Duniho Jun 18 '17 at 07:15
  • OP, I think you have jumped to conclusions about root cause. Can you please more clearly explain the *symptoms* you are seeing? C# is perfectly able to handle safe array access. – John Wu Jun 18 '17 at 07:30
  • By any chance you want `.Select(e => e.Length).Sum()`? – Christian Gollhardt Jun 18 '17 at 07:34
  • The full source code is provided; just install mono-complete and run make. – TamaMcGlinn Jun 18 '17 at 07:39

1 Answers1

1

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];
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64