I\'ve been storing c pointers in Java through JNI by following the advice of @tulskiy in this post Passing pointers between C and Java through JNI
The trick is to ca
There are various handy integer types in C. The one you want is probably intptr_t or uintptr_t:
return (jlong)(intptr_t) ptr;
The difference?
intptr_t
to jlong
and back is guaranteed to work provided jlong
is big enough (which you're implicitly assuming it is anyway).uinttptr_t
to jlong
and back avoids a sign-extension, but is undefined behaviour if the uintptr_t
is too big to fit in a jlong
(but all "sane" architectures/compilers just use two's complement arithmetic)Try casting with intptr_t (store a pointer regardless of the platform capacity).