Passing an integer aray fron a c func

Discussion in 'Java' started by ss, Jun 22, 2006.

  1. ss

    ss Guest

    hi frnds i want to return an integer array fron c prog to java how do i

    proceed with it .....i tried with some code but is is not
    working......please tell me wat to do and where i m wrong
    my code is
    class IntArray {
    private native int[] sumArray(int[] arr);
    public static void main(String[] args) {
    IntArray p = new IntArray();
    int arr[] = new int[10];
    int newarr[]=new int[10];
    System.out.println("previous values of newarr");
    for(int j=0;j<10;j++)
    {arr[j]=j;
    System.out.println(arr[j]);
    }
    newarr=p.sumArray(arr);

    //for (int i = 0; i < 10; i++)
    // {
    //arr = i;
    //}
    // int sum = p.sumArray(arr,newarr);
    System.out.println("new values of newarr");
    try{for(int j=0;j<10;j++)
    System.out.println(newarr[j]);
    }
    catch(NullPointerException np){}
    System.out.println("value of sum");
    // System.out.println("sum = " + sum);
    }
    static {
    System.loadLibrary("IntArray");
    }



    }


    #include<stdio.h>
    #include<jni.h>
    #include"IntArray.h"
    JNIEXPORT jintArray JNICALL Java_IntArray_sumArray
    (JNIEnv *env, jobject obj, jintArray arr)
    {
    jint buf[10];
    jint i, sum = 0;
    (*env)->GetIntArrayRegion(env, arr, 0, 10, buf);
    for (i = 0; i < 10; i++) {
    buf= buf+2;
    printf("%d",buf);
    }
    return buf;
    }

    it is printing the values in c program but it is not retuning the
    newarr
     
    ss, Jun 22, 2006
    #1
    1. Advertising

  2. On 21 Jun 2006 22:46:00 -0700, ss wrote:
    > hi frnds i want to return an integer array fron c prog to java how do i
    > proceed with it .....i tried with some code but is is not
    > working......please tell me wat to do and where i m wrong


    [...]

    > #include<stdio.h>
    > #include<jni.h>
    > #include"IntArray.h"
    > JNIEXPORT jintArray JNICALL Java_IntArray_sumArray
    > (JNIEnv *env, jobject obj, jintArray arr)
    > {
    > jint buf[10];
    > jint i, sum = 0;
    > (*env)->GetIntArrayRegion(env, arr, 0, 10, buf);
    > for (i = 0; i < 10; i++) {
    > buf= buf+2;
    > printf("%d",buf);
    > }
    > return buf;
    > }
    >
    > it is printing the values in c program but it is not retuning the
    > newarr


    Your code does not return any new array, it returns a pointer to a
    buffer local to the C method. There are two serious problems with
    this: you pass a pointer to a C int array, not the jintArray expected
    by the caller, and it goes out of scope when you return from the
    method. Either one of these errors can be expected to crash your JVM.

    If you want to return a new array, use NewIntArray() and initialize
    the contents of the array with e.g. SetIntArrayRegion().

    If you simply want to modify the contents of the existing array, use
    SetIntArrayRegion(). Or use GetIntArrayElements(), modify the
    resulting C int array, and then ReleaseIntArrayElements() to propagate
    your changes back to the original jintArray.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jun 22, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CRON
    Replies:
    24
    Views:
    203,766
    Adrienne Boswell
    Jun 20, 2006
  2. Johnny
    Replies:
    3
    Views:
    482
    Robert Kern
    Aug 23, 2005
  3. Hari Sekhon
    Replies:
    0
    Views:
    556
    Hari Sekhon
    Jun 20, 2006
  4. JoeC
    Replies:
    12
    Views:
    657
  5. Replies:
    5
    Views:
    242
    Ian Macdonald
    Dec 15, 2003
Loading...

Share This Page