How can I do to share the array elements between "C" and "Java"

Discussion in 'Java' started by Ejeep, Nov 6, 2007.

  1. Ejeep

    Ejeep Guest

    Hi,all

    I want to implement the function about this: c and java between JNI
    can share the same data area. For example, when change the value of
    the array in java, i can get the same value in c.

    I use the function:Xxx* GetXxxArrayElements(JNIEnv env, jarray array,
    jboolean isCopy)

    But i find that the "isCopy" is JNI_TRUE which means that i get the
    pointer which point to a copy,so i can't get the change
    in Java later.

    How can I do?Can i set "isCopy" with JNI_FALSE??

    Thank u.
    Ejeep, Nov 6, 2007
    #1
    1. Advertising

  2. On Mon, 05 Nov 2007 18:35:57 -0800, Ejeep wrote:
    > I use the function:Xxx* GetXxxArrayElements(JNIEnv env, jarray
    > array, jboolean isCopy)
    >
    > But i find that the "isCopy" is JNI_TRUE which means that i get the
    > pointer which point to a copy,so i can't get the change in Java
    > later.
    >
    > How can I do?Can i set "isCopy" with JNI_FALSE??


    You can't make any meaningful change to "isCopy", it's purpose is just
    to inform the caller.

    However when you release the copy (which you must do), the changes
    will propagate to the Java array: see ReleaseXxxArrayElements().

    Or you could use GetPrimitiveArrayCritical() (and corresponding
    Release...), which claim to make it "more likely" that you get a
    direct pointer, but then there are additional restrictions on what you
    can do while you are holding the array. YMMV.

    /gordon

    --
    Gordon Beaton, Nov 6, 2007
    #2
    1. Advertising

  3. On 06 Nov 2007 06:59:14 GMT, Gordon Beaton wrote:
    > You can't make any meaningful change to "isCopy", it's purpose is just
    > to inform the caller.


    Agh! "Its purpose", of course. It's early here.

    /gordon

    --
    Gordon Beaton, Nov 6, 2007
    #3
  4. Ejeep

    Piotr Kobzda Guest

    Ejeep wrote:

    > I want to implement the function about this: c and java between JNI
    > can share the same data area. For example, when change the value of
    > the array in java, i can get the same value in c.
    >
    > I use the function:Xxx* GetXxxArrayElements(JNIEnv env, jarray array,
    > jboolean isCopy)


    You may also try to use a /direct buffers/ to achieve your goal.

    See:
    <http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions.html#nio_support>


    piotr
    Piotr Kobzda, Nov 6, 2007
    #4
  5. Ejeep

    Ejeep Guest

    Gordon Beaton wrote:

    > Or you could use GetPrimitiveArrayCritical() (and corresponding
    > Release...), which claim to make it "more likely" that you get a
    > direct pointer, but then there are additional restrictions on what you
    > can do while you are holding the array. YMMV.
    >
    > /gordon
    >
    > --


    Thank u for you reply.
    I use GetPrimitiveArrayCritical() directly in an example code ,and it
    is useful.
    But in my project ,it appears the error:"fixing up unaligned userspace
    access"

    How can i fix this???
    Ejeep, Nov 6, 2007
    #5
  6. Ejeep

    Ejeep Guest

    On Nov 6, 9:15 pm, Piotr Kobzda <> wrote:

    > You may also try to use a /direct buffers/ to achieve your goal.
    >
    > See:
    > <http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions...>
    >
    > piotr


    Thank you for you reply.

    I will try it.
    By the way,the sun's spec doesn't contain an example.
    Sign!~~~
    Ejeep, Nov 6, 2007
    #6
  7. On Tue, 06 Nov 2007 06:11:46 -0800, Ejeep wrote:
    > I use GetPrimitiveArrayCritical() directly in an example code ,and
    > it is useful. But in my project ,it appears the error:"fixing up
    > unaligned userspace access"
    >
    > How can i fix this???


    This is not a Java or even a JNI issue. You appear to invoking a
    system call from your native code, and are passing a pointer that is
    not sufficiently aligned for the specific datatype it refers to. An
    example of this is attempting to refer to an "int" at an odd address,
    when the architecture requires every "int" to be aligned at an even
    address.

    /gordon

    --
    Gordon Beaton, Nov 6, 2007
    #7
  8. Ejeep

    Lew Guest

    Gordon Beaton wrote:
    > On 06 Nov 2007 06:59:14 GMT, Gordon Beaton wrote:
    >> You can't make any meaningful change to "isCopy", it's purpose is just
    >> to inform the caller.

    >
    > Agh! "Its purpose", of course. It's early here.


    You're my hero!

    --
    Lew
    Lew, Nov 6, 2007
    #8
  9. Ejeep

    Ejeep Guest

    On Nov 6, 9:15 pm, Piotr Kobzda <> wrote:
    > Ejeep wrote:
    > > I want to implement the function about this: c and java between JNI
    > > can share the same data area. For example, when change the value of
    > > the array in java, i can get the same value in c.

    >
    > > I use the function:Xxx* GetXxxArrayElements(JNIEnv env, jarray array,
    > > jboolean isCopy)

    >
    > You may also try to use a /direct buffers/ to achieve your goal.
    >
    > See:
    > <http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions...>
    >
    > piotr


    Hi,Piotr
    I try to use JNI function: GetDirectBufferAddress(),
    GetDirectBufferCapacity()
    and I get the return value with "NULL" and "-1".


    test code:
    ------------------------------
    JNIEXPORT void JNICALL Java_test_nativeInit
    (JNIEnv *env, jclass obj, jintArray arr)
    {

    jlong jl;
    char* ch;

    ch = (char*)(*env)->GetDirectBufferAddress(env,arr);
    jl = (*env)->GetDirectBufferCapacity(env,arr);

    return;
    }
    Ejeep, Nov 7, 2007
    #9
  10. Ejeep

    Ejeep Guest

    On Nov 6, 10:34 pm, Gordon Beaton <> wrote:
    > On Tue, 06 Nov 2007 06:11:46 -0800, Ejeep wrote:
    > > I use GetPrimitiveArrayCritical() directly in an example code ,and
    > > it is useful. But in my project ,it appears the error:"fixing up
    > > unaligned userspace access"

    >
    > > How can i fix this???

    >
    > This is not a Java or even a JNI issue. You appear to invoking a
    > system call from your native code, and are passing a pointer that is
    > not sufficiently aligned for the specific datatype it refers to. An
    > example of this is attempting to refer to an "int" at an odd address,
    > when the architecture requires every "int" to be aligned at an even
    > address.
    >
    > /gordon
    >
    > --


    Thank you.

    It's hard for me to check the "aligned" question.
    The variable I use is the same type "int".
    Have any methods to check?

    ----
    ejeep
    Ejeep, Nov 7, 2007
    #10
  11. On Tue, 06 Nov 2007 18:45:34 -0800, Ejeep wrote:
    > It's hard for me to check the "aligned" question.
    > The variable I use is the same type "int".
    > Have any methods to check?


    No, but "proper" use of pointers won't cause this problem. It can only
    occur when you cast pointers from one type to another, and they have
    different alignment requirements (e.g. from void* or char* to int*).

    Again, this isn't a Java or JNI issue.

    /gordon

    --
    Gordon Beaton, Nov 7, 2007
    #11
  12. Ejeep

    Ejeep Guest

    On Nov 7, 2:50 pm, Gordon Beaton <> wrote:
    > On Tue, 06 Nov 2007 18:45:34 -0800, Ejeep wrote:
    > > It's hard for me to check the "aligned" question.
    > > The variable I use is the same type "int".
    > > Have any methods to check?

    >
    > No, but "proper" use of pointers won't cause this problem. It can only
    > occur when you cast pointers from one type to another, and they have
    > different alignment requirements (e.g. from void* or char* to int*).
    >
    > Again, this isn't a Java or JNI issue.
    >
    > /gordon
    >
    > --


    I see.
    it isn't a Java or JNI issue
    Just don't find the solution.

    Thank u very much
    Ejeep, Nov 7, 2007
    #12
  13. Ejeep

    Piotr Kobzda Guest

    Ejeep wrote:

    > I try to use JNI function: GetDirectBufferAddress(),
    > GetDirectBufferCapacity()
    > and I get the return value with "NULL" and "-1".


    Most likely your JVM do not support that. What is the JVM's version,
    vendor, and the OS you're trying to use that with?


    piotr
    Piotr Kobzda, Nov 7, 2007
    #13
    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. Mironov Leonid

    Share memory between C and Java

    Mironov Leonid, Jul 27, 2003, in forum: Java
    Replies:
    5
    Views:
    4,541
    Peter Szymanski
    Jul 28, 2003
  2. K. Jansma
    Replies:
    7
    Views:
    317
    Duncan Booth
    Feb 21, 2006
  3. Saraswati lakki
    Replies:
    0
    Views:
    1,287
    Saraswati lakki
    Jan 6, 2012
  4. Jack
    Replies:
    2
    Views:
    201
  5. Eric Wong
    Replies:
    0
    Views:
    193
    Eric Wong
    Feb 13, 2010
Loading...

Share This Page