What am I doing: I add four integers in C. on the way, I lose information.
See code below:
//c-file
jbyte *inputByteArray = (*env)->GetDirectBufferAddress (env, obj);
// checked every value, also sizeof(val1)= 4 etc...
int val1 = (int) *(inputByteArray + 1); //120
int val2 = (int) *(inputByteArray + 2); //120
int val3 = (int) *(inputByteArray + 3); //180
int val4 = (int) *(inputByteArray + 4); //180
int result = val1 + val2 + val3 + val4;
return result;
//return type is int
//output: 88, should be 600
// 88 binary: 0000 0000 0101 1000
//600 binary: 0000 0010 0101 1000
The special thing about this is the following, which might be causing the problem:
The 4 values for the input are from a handed-over Buffer from Java, which is a direct ByteBuffer. It is directly allocated in Java, in order NOT be moved by the garbage collector. On the c-side I hand the buffer over via pointer from "GetDirectBufferAddress" (see code), and the single values do match the values in the array.
Does anyone know about this strange behaviour?
When I am using IntBuffer to hand over the numbers, it works by the way.
Im working here on performance, so I want small buffers and my data values are small enough to use ByteBuffer. (this is only a fragment of a larger calculation on the c-code side)
Since this is on Android, I did not manage to debug into the c-code...
Edit: I am using eclipse/SDK/NDK in current versions on Android 3.2.1 testing device