kmp kmp - 1 month ago 21
C Question

How to use a struct with a struct in jnr ffi

I have the following c code:

#include <stdio.h>

struct Second {
int a_number;
};

struct Top {
struct Second second;
};

void lets_go(struct Top *top) {
printf("The number is %d\n", top->second.a_number);
}


And I want to do this except from Java:

int main(void) {
struct Top top = {{8}};
lets_go(&top);
}


I also want to use the super cool looking jnr-ffi stuff, so I looked at the tests and tinkered and ended up with this:

package structs.playing;

import structs.playing.Program.Test.Top;
import structs.playing.Program.Test.Second;
import jnr.ffi.LibraryLoader;
import jnr.ffi.Runtime;
import jnr.ffi.Struct;

public final class Program {

public static interface Test {

void lets_go(Top top);

public static final class Second extends Struct {
public final Signed32 a_number = new Signed32();
public Second(final Runtime runtime) {
super(runtime);
}
}

public static final class Top extends Struct {
public Second second;
public Top(final Runtime runtime) {
super(runtime);
}
}
}

public static void main(final String[] args) {

Test test = LibraryLoader.create(Test.class).load("test");
Runtime runtime = Runtime.getRuntime(test);
Top top = new Top(runtime);
Second second = new Second(runtime);
top.second = second;
second.a_number.set(7);
test.lets_go(top);
}
}


The problem is that the value of
a_number
is not set at all so I get a junk value in the output, for example:

The number is 46645760


So how do I get the same as in my C code?

kmp kmp
Answer

I have figured it out (by the way, I am aware that the members should be private and wrapped in properties but I wanted to make the code snippet as small as possible, this is not production quality code)...

If you put a Pointer member variable into the struct you can use it's memory when you construct the sub-ordinate Struct like so...

package structs.playing;

import structs.playing.Program.Test.Top;
import jnr.ffi.LibraryLoader;
import jnr.ffi.Runtime;
import jnr.ffi.Struct;

public final class Program {

    public static interface Test {

        void lets_go(Top top);

        public static final class Second extends Struct {

            public final Signed32 a_number = new Signed32();

            public Second(final Runtime runtime) {
                super(runtime);
            }           
        }

        public static final class Top extends Struct {

            private final Pointer secondPointer = new Pointer();            
            public final Second second;

            public Top(final Runtime runtime) {
                super(runtime);                             
                second = new Second(runtime); 
                second.useMemory(secondPointer.getMemory());
            }           
        }
    }

    public static void main(final String[] args) {

         Test test = LibraryLoader.create(Test.class).load("test");
         Runtime runtime = Runtime.getRuntime(test);         
         Top top = new Top(runtime);
         top.second.a_number.set(8);         
         test.lets_go(top);
    }
}