Using Java longs as pointers

I have seen in other JNI code and on the forms here that use of long is the recomendation for pointers. However, when I use them with the C dll I'm wrapping I get compile warnings "warning: cast to pointer from integer of different size". The pointers are to a variety of C structs and there is nothing in the header file that defines the pointers as any specific size. An example is as follows:

JNIEXPORT jlong JNICALL Java_jnewton_JNBody_nbCreateBody

(JNIEnv * env, jobject object, jlong newtonWorld, jlong newtonCollision){

jlong results;

NewtonWorld *nw = (NewtonWorld*)newtonWorld;

NewtonCollision *col = (NewtonCollision*)newtonCollision;

NewtonBody *body = NewtonCreateBody(nw, col);

results = (jlong)body;

return(results);

}

Are the dll functions returning int sized pointers that I don't see? I am using the single precision dll as recommended by the creator for max speed. Is this the basis of the problem?

Jim

[1089 byte] By [jjones3566a] at [2007-11-26 21:59:54]
# 1

Hi,

You should remember of your JNative experience that all pointers are unsigned jint.

So using jint in Java size is enough but do not try to have mathematical stuff on them (sometimes they are negative jint).

I am sorry that JNative did not work for you.

--Marc (http://jnative.sf.net)

mdentya at 2007-7-10 3:59:16 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

> NewtonWorld *nw = (NewtonWorld*)newtonWorld;

newtonWorld is a long.

nw is a pointer.

The warning indicates that the long has more bits than the pointer. Which it does.

You should be able to remove the warning by looking into the preprocess (probably something like #warn). Comment the code significantly that you are removing the warning because it is not relevant in this case.

jschella at 2007-7-10 3:59:17 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

Hi, Marc, finally got to where I understand how to make JNI work so thought I'd try it again. As to your point:

On this forum ther are several posts that advocate the use of Java longs as the storage types for C/C++ pointer. In looking through the J3D code there are numerous pointers to structures and pointers to members that uniformally are assigned to jlongs. An example:

jlong ctx = ctxProperties->context;

My problem is that I was trying to cast jlong to a C pointer and the C compiler recognized as an int typ which it could convert to a C long but threw the warning because I should have explicitly cast it myself rather than make the compiler do it. So I did this:

NewtonWorld *nw = (NewtonWorld*)((long)newtonWorld);

as opposed to my previous code of:

NewtonWorld *nw = (NewtonWorld*)newtonWorld;

this made the warnings go away. Thanks for you interest and help.

Jim

jjones3566a at 2007-7-10 3:59:17 > top of Java-index,Java HotSpot Virtual Machine,Specifications...