user3468999
user3468999

Reputation: 249

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

Upvotes: 2

Views: 186

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545766

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.

Upvotes: 2

Related Questions