Search code examples
c++java-native-interfacelong-integer

Casting pointers into long


I'm working with JNI (developing for Android using Native code).

The case is I would like to send to a Java Class a pointer to a Native object. For that I've seen in casting the pointer direction into a long, like this:

long pointerDirection = (long)pointer;

Is this secure? I've read that in some architectures, a 'long' is just 32 bits, but the pointers just need 32 bits, right? Or in 64 bits they also use 64 bits?

Thanks


Solution

  • Is this secure?

    No, definitely not – the standard makes no such guarantee (even though it may work on many machines in practice).

    You can cast it safely (only) to the integral type ptrdiff_t.