Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid jni datatype losing precision (ByteBuffer, int)
    primarykey
    data
    text
    <p>What am I doing: I add four integers in C. on the way, I lose information.</p> <p>See code below:</p> <pre><code>//c-file jbyte *inputByteArray = (*env)-&gt;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 </code></pre> <p>The special thing about this is the following, which might be causing the problem:</p> <p>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.</p> <p>Does anyone know about this strange behaviour?</p> <p>When I am using IntBuffer to hand over the numbers, it works by the way. <br/> 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)</p> <p>Since this is on Android, I did not manage to debug into the c-code...</p> <p>Edit: I am using eclipse/SDK/NDK in current versions on Android 3.2.1 testing device</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload