Hi all, I need to invoke from Java a C function that takes, as input, a bidimensional int array. The same array is used by the C function to provide its output. I modeled the bidimensional array as an array of int* using the "array_functions" directive available with SWIG:

%array_functions(int*, intArray);

As expected, SWIG generates the SWIGTYPE_p_p_int type plus following two helper functions:

public static int[] intArray_getitem(SWIGTYPE_p_p_int ary, int index) {
  return contrastJNI.intArray_getitem(SWIGTYPE_p_p_int.getCPtr(ary), index);

public static void intArray_setitem(SWIGTYPE_p_p_int ary, int index, int[] value) {
  contrastJNI.intArray_setitem(SWIGTYPE_p_p_int.getCPtr(ary), index, value);

I am able to allocate from Java the SWIGTYPE_p_p_int class and to write into it. However, as soon as I try (from Java) to access the content of the array (like in the following example), I get a segmentation fault exception:

            SWIGTYPE_p_p_int abc = contrast.new_intArray(1000);
            contrast.intArray_setitem(abc, 1, new int[100]);
            // invoke the C function passing it 'abc'
            int[] def = contrast.intArray_getitem(abc, 1);

As far I can see, the C function is able to read and write 'abc', without any problem. Thus I tried a second code, where I took off the C part, and I still get the same error:

            SWIGTYPE_p_p_int abc = contrast.new_intArray(1000);
            contrast.intArray_setitem(abc, 1, new int[100]);
            int[] def = contrast.intArray_getitem(abc, 1);
Is there anybody who can help me with this?

Thank you in advance!

Note: I posted the same question on stackoverflow a couple of weeks ago (see http://stackoverflow.com/questions/25139171/passing-multidimensional-int-array-from-java-to-c-and-back-using-swig)

Umberto Ferraro Petrillo
Dipartimento di Scienze Statistiche
Universita' di Roma -- La Sapienza
P.le Aldo Moro, 5
00185 - Rome, Italy
phone: +39 06 499 10513
e-mail: Umberto.Ferraro@uniroma1.it