Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Passing an integer aray fron a c func

Thread view: 
ss - 22 Jun 2006 06:46 GMT
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] = 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[i]= buf[i]+2;
                printf("%d",buf[i]);
    }
    return buf;
}

it is printing the values in c program but it is not retuning the
newarr
Gordon Beaton - 22 Jun 2006 07:20 GMT
> 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>
[quoted text clipped - 14 lines]
> 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

Signature

[  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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.