1

次の情報源について質問がありますが、少しわかりやすくするために簡略化しました。

cコード

struct test
{
int test1;
};

int create_context(test **context); 
int use_context(test *context);

Javaコード

public static class test extends Structure {
    public int test1;
    public test() {
        super();
    }
    public test()(Pointer p) {
        super(p);
    }
    protected List getFieldOrder() {
        return Arrays.asList("test1");
    }
    public test(int test1) {
        super();
        this.test1 = test1;
    }
    public static class ByReference extends test implements Structure.ByReference {

    };
    public static class ByValue extends test implements Structure.ByValue {

    };
}
public static native int create_context(PointerByReference context);
public static native int use_context(TestLibrary.test context);

私はこのようにJavaの構造にアクセスします、

    PointerByReference contextPointer = new PointerByReference();
    int status = INSTANCE.create_context(contextPointer);
    test context = new test(contextPointer.getValue());
    status = INCTANCE.use_context(context);

Visual Studioでこれをデバッグしたとき、create_contextとuse_contextに異なるメモリアドレスが使用されていることを確認しました。int test1の値を設定すると、それは正しいのですが、なぜコンテキストのアドレスが異なるのか疑問に思います。誰かアイデアがありますか?それはメモリの問題につながりませんでしたか?または、私が間違っていることについて何か考えはありますか?ありがとうバレンチナ

4

1 に答える 1

1

あなたは一般的に使用する規則を選択したstruct test*ので、それを使用します。

ネイティブコードは次のようになっている必要があります。

int create_context(struct test **context) {
    *context = (struct test *)malloc(sizeof(test));
    // initialize here...
    return 0;
}

を呼び出すときは、ポインタのアドレスcreate_contextを渡す必要があります。

struct test* test_ptr;

create_context(&test_ptr);

test_ptr->some_field = ...; // operate on your struct via pointer

struct test値( )、参照(struct test*)、または参照のアドレス()で構造を使用する場合は、まっすぐに保つことが重要ですstruct test**。使用法がCであろうとJavaであろうと、概念は同じです。

于 2013-03-24T23:11:58.797 に答える