android - The address of the pointer changes when passed to a C function below the JNI layer -


i trying write native c code in android using ndk. have function segment_offline() takes in many arguments including integer pointer int * num_segments. want pointer because want read value assigned the integer pointer pointing in c function acts jni interface. here code segment_offline():

int * segment_offline(const segment_datatype *x, const int num_samples_x, const float *svara_cents, const int *svara_lengths, const int num_svaras, const int type, const int hop_size, const float tonic, int *num_segments) {  int i;  // using dtw ... if (0 == type) {      filtered_samples_t fns;      // removing negative frequencies in hz.     filter_negative_samples(x, num_samples_x, &fns);      __android_log_print(android_log_debug, "logndk", "after filter_negative_samples\n");      // converting hz cents     convert_pitch_to_cents(fns.arr_filtered, fns.len, tonic);      __android_log_print(android_log_debug, "logndk", "after convert_pitch_to_cents\n");      //print_double_array(fns.arr_filtered, fns.len);     segments_t seg = segment_offline_dtw(fns.arr_filtered, fns.len, svara_cents, svara_lengths, num_svaras, hop_size);      __android_log_print(android_log_debug, "logndk", "after segment_offline_dtw\n");      int * ret_segments = (int *) malloc(sizeof(seg.num_segments));      __android_log_print(android_log_debug, "logndk", "after allocating memory ret_segments\n");      // setting value of number of segments in pointer referred.     // ########## segmentation fault here #########     __android_log_print(android_log_debug, "logndk", "the number of segments are: %d\n", seg.num_segments);     *num_segments = seg.num_segments;      __android_log_print(android_log_debug, "logndk", "after assigning number of segments: %d\n", *num_segments);      (i = 0;i < seg.num_segments * 2; i++) {         ret_segments[i] = fns.original_indices[seg.segments_pos[i]];         printf("%d, ", ret_segments[i]);     }     printf("\n");      __android_log_print(android_log_debug, "logndk", "returning segment_offline\n");      return ret_segments; } 

the jni c function have written is:

jniexport jintarray jnicall java_com_camut_audioiolib_dsp_dspalgorithms_segmentoffline   (jnienv *env, jclass cls, jdoublearray x, jint numsamples, jfloatarray svaracents,   jintarray svaralengths, jint numsvaras, jint type, jint hopsize, jfloat tonic) {      jdouble *xdouble = (*env)->getdoublearrayelements(env, x, null);     jfloat *svaracentsfloat = (*env)->getfloatarrayelements(env, svaracents, null);     jint *svaralengthsint = (*env)->getintarrayelements(env, svaralengths, null);      __android_log_print(android_log_debug, "logndk", "the size of type tonic is: %d", sizeof(tonic));      int num_segments;      int * segments = (int *) segment_offline(xdouble, numsamples, svaracentsfloat, svaralengthsint,         numsvaras, type, hopsize, tonic, &num_segments);      if (null == segments) {        return null;     }      // releasing allocated memories ...     (*env)->releasedoublearrayelements(env, x, xdouble, jni_abort);     (*env)->releasefloatarrayelements(env, svaracents, svaracentsfloat, jni_abort);     (*env)->releaseintarrayelements(env, svaralengths, svaralengthsint, jni_abort);       return null;    } 

you see in above code have initialized num_segments int variable , passing address of variable function mentioned above.

so, seems unable derefer int pointer. suggestions on this. need pointer because returning other value function return value.

just guess. corrupts num_segments before derefer. try print value @ function begin , before crash line. crash log shows sigsegv occurs on address 0x60000000, unlikely address of stack object.

edit

also incorrect argument value may caused call incorrectly declared function, i.e. if have call function not defined yet or defined in translation unit - must provide forward declaration tells compiler actual argument list , return type. note c89 allows implicit function declaration. in case if don't declare function - compiler supposes has int return type , takes number of arguments. see here details. note, mismatch of definition , call signatures causes undefined behavior.

p.s. absence of forward declaration may force cast function return since compiler expects int.


Comments