Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7723769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:31:30+00:00 2026-06-01T04:31:30+00:00

I want to access an array that is created and updated in the native

  • 0

I want to access an array that is created and updated in the native C code efficiently. If need be, i could send a pointer or reference from Java code to the native side and have the C-code populate it so that i can read it from SDK side when it’s ready to be consumed.

Currently, this is how i am doing it. But i think there can be better ways to do it, since i am doing one copy in C-side and then there an object that is created every time i issue a read on the Java-side.

My Java code:

double[] valuesFromNative = getValues();
public static native double[] getValues();
static { System.loadLibrary("test-jni"); }

My native (C and not C++) code:

#define LEN 18
double testDoubleArr[LEN];
jdoubleArray Java_com_test_testActivity_getValues(JNIEnv *env, jclass clazz) {
    jboolean isCopy;
    int i;
    jdoubleArray result = (*env)->NewDoubleArray(env, LEN);
    jdouble* destArrayElems = (*env)->GetDoubleArrayElements(env, result, &isCopy);
    for (i = 0; i < LEN; i++) {
       destArrayElems[i] = testDoubleArr[i];
    }
    if(isCopy == JNI_TRUE) {
        //  isCopy should NEVER be JNI_TRUE in this case, right?
        // so, i could as well replace this condition with
        // assert(isCopy == JNI_FALSE)?
    }
return result;
}

This code snippet works – so, i am looking at more efficient or rather correct way to achieve the same thing.

Thanks for sharing your thoughts.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:31:31+00:00Added an answer on June 1, 2026 at 4:31 am

    I think SetDoubleArrayRegion() would be faster. Less code and less JNI calls, that’s for sure.

    jdoubleArray result = (*env)->NewDoubleArray(env, LEN); 
    (*env)->SetDoubleArrayRegion(env, result, 0, LEN, testDoubleArr);
    

    You don’t even have to create the array on the C++ side. Declare the method like this:

    public static native void getValues(double[] a);   
    

    Implement like this:

    void Java_com_test_testActivity_getValues(JNIEnv *env, jclass clazz, jdoubleArray a)
    {//...
    

    Create the array on the Java side, cache it in an instance variable or something, and pass it to JNI to be filled whenever needed. Make sure the assumptions about array size are the same on the Java side and on the C side.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I've created an array in c# and want to access the values in
I want to add elements to arrays within an object that I access like
i want to access a integer and a string from a class to all
I want to access a MySQL database and I want to read+write data from+to
I am trying to access HTML elements by their id from an array of
I want to access the data of table using WCF services. My code is
I need to display a page in drupal that is created by a template
I just wrote a delegate method, that shall return an array, I've created inside
PROBLEM: I'm trying access binary records that were created in Borland Delphi and stored
I want to create a dynamic 2d array by function but it seems that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.