Passing pointers between C and Java through JNI

后端 未结 2 1009
清歌不尽
清歌不尽 2021-01-02 04:37

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

相关标签:
2条回答
  • 2021-01-02 05:23

    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?

    • Casting from intptr_t to jlong and back is guaranteed to work provided jlong is big enough (which you're implicitly assuming it is anyway).
    • Casting from 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)
    0 讨论(0)
  • 2021-01-02 05:28

    Try casting with intptr_t (store a pointer regardless of the platform capacity).

    0 讨论(0)
提交回复
热议问题