我的c++函数是这样定义的:
extern "C" int add(int* a,int* b,int* c)
{ a = new int[10];
b = new int[10];
c = new int[10];
for(int i=0;i<10;i++)
{
c[i]=a[i]+b[i];
} return 1;
}在Java中调用的代码如下: JNative t = new JNative("Test.dll", "add");
Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4*10));
Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4*10));
Pointer c = new Pointer(MemoryBlockFactory.createMemoryBlock(4*10));
for(int i=0;i<10;i++){
a.setIntAt(4*i, i);
b.setIntAt(4*i, i);
}
t.setRetVal(Type.INT);
t.setParameter(0,a);
t.setParameter(1, b);
t.setParameter(2, c);
t.invoke();
//输出c指针指向的数组的值
for(int j=0;j<10;j++){
System.out.println(c.getAsInt(4*j));
}
//输出函数返回值
System.out.println("return value="+t.getRetValue());
a.dispose();
b.dispose();
t.dispose();
执行结果:
0
0
0
0
0
0
0
0
0
0
1
函数的返回值已经得到了,可是c指针里的是空值,不知道为什么?请懂的朋友给点提示,该怎么解决这个问题?
extern "C" int add(int* a,int* b,int* c)
{ a = new int[10];
b = new int[10];
c = new int[10];
for(int i=0;i<10;i++)
{
c[i]=a[i]+b[i];
} return 1;
}在Java中调用的代码如下: JNative t = new JNative("Test.dll", "add");
Pointer a = new Pointer(MemoryBlockFactory.createMemoryBlock(4*10));
Pointer b = new Pointer(MemoryBlockFactory.createMemoryBlock(4*10));
Pointer c = new Pointer(MemoryBlockFactory.createMemoryBlock(4*10));
for(int i=0;i<10;i++){
a.setIntAt(4*i, i);
b.setIntAt(4*i, i);
}
t.setRetVal(Type.INT);
t.setParameter(0,a);
t.setParameter(1, b);
t.setParameter(2, c);
t.invoke();
//输出c指针指向的数组的值
for(int j=0;j<10;j++){
System.out.println(c.getAsInt(4*j));
}
//输出函数返回值
System.out.println("return value="+t.getRetValue());
a.dispose();
b.dispose();
t.dispose();
执行结果:
0
0
0
0
0
0
0
0
0
0
1
函数的返回值已经得到了,可是c指针里的是空值,不知道为什么?请懂的朋友给点提示,该怎么解决这个问题?
c.dispose()好像没有但是c的代码不合格,明显指针没学好
a = new int[10];
b = new int[10];
c = new int[10];
这三句一加,abc指向了别的地方,传入的参数地址当然会无效
既然传入参数都已经分配了空间,再new就是画蛇添足