2011-10-04 3 views
0

더블 16 배열을 사용하는 매우 간단한 Java 프로그램에서 네이티브 C 호출로 전달합니다. C 함수에서 배열의 각 요소를 가져 와서 합계를 반환합니다. 나는 온라인에서 몇 가지 예제를 따라 갔고 배열로 무엇이 있는지에 상관없이 각각의 결과가 반환 된 곳으로 1717986916이 들어갔다. 내가 뭘 잘못하고 있는거야? 여기에 내 활동과 C 코드가 있습니다.네이티브 호출의 결과는 항상 반환됩니다. 1717986916

public class NDKFooActivity extends Activity implements OnClickListener { 
    // load the library - name matches jni/Android.mk 
    static { 
     System.loadLibrary("ndkfoo"); 
    } 

    // declare the native code function - must match ndkfoo.c 
    public static native int sumFIR(double[] arr); 

    private TextView textResult; 
    private Button buttonGo; 
    private double[] dList = new double[16]; 
    private List<Double> list = new LinkedList<Double>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     textResult = (TextView) findViewById(R.id.textResult); 
     buttonGo = (Button) findViewById(R.id.buttonGo); 
     buttonGo.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View view) { 
     String out = ""; 

     ///////////////////////////////////// 
     //load first 16 data sets 

     list.add(2135.1); list.add(1130.1); list.add(2530.1); list.add(2430.1); 
     list.add(2330.1); list.add(1940.1); list.add(1210.1); list.add(2100.1); 
     list.add(2095.1); list.add(2105.1); list.add(2000.1); list.add(1876.1); 
     list.add(1852.1); list.add(1776.1); list.add(1726.1); out += "" + add(1716.1); 
     ///////////////////////////////////// 

     out += "\n" + add(2135.1);   out += "\n" + add(1130.1); 
     out += "\n" + add(2530.1);   out += "\n" + add(2430.1); 
     textResult.setText(out); 
    } 

    public double add(double object) { 
     if (list.size() > 15) { 
      list.remove(0); 
     } 
     list.add(object); 
     for (int i=0; i< 16; i++) { 
      dList[i] = list.get(i).doubleValue(); 
     } 

     double dResult = sumFIR(dList); 
     return dResult; 
    } 
} 

ndkfoo.c은 다음과 같습니다

#include <stdio.h> 
#include <stdlib.h> 
#include <jni.h> 

jdouble Java_com_nsf_ndkfoo_NDKFooActivity_sumFIR (JNIEnv* env, jobject obj, jdoubleArray arr) { 
    jdouble result = 0; 
    // initializations, declarations, etc 
    jint i = 0; 

    // get a pointer to the array 
    jdouble *c_array = (*env)->GetDoubleArrayElements(env, arr, 0); 
    jsize len = (*env)->GetArrayLength(env, arr); 

    for (i=0; i<16; i++){ 
     result = result + c_array[i]; 
    } 

    // release the memory so java can have it again 
    (*env)->ReleaseDoubleArrayElements(env, arr, c_array, 0); 

    // return something, or not.. it's up to you 
    return result; 
} 

답변

0

좋아 함수가 INT를 사용하는 대신에 두 번이라고 할 대답은 네이티브 자바를 밝혀 냈다. 그것이 거의 항상 같은 번호를 반환 이유는 모르겠다.

// declare the native code function - must match ndkfoo.c 
public static native int sumFIR(double[] arr); 

이어야한다

public static native double sumFIR(double[] arr); 
관련 문제