nativelibs4java / BridJ

BridJ: blazing fast Java / C / C++ interop
https://code.google.com/archive/p/bridj/
Other
299 stars 77 forks source link

java.lang.RuntimeException: Unaligned pointer access #72

Open rhz opened 9 years ago

rhz commented 9 years ago

Hi, I get this error when trying to access a field of a pointer to a struct returned from C function call. The C code is the following:

#include <stdio.h>

typedef struct S {
  int a;
  double b;
} S;

S* printHello(S* s) {
  printf("Hello!\n");
  return(s);
}

I'm compiling it with gcc -Wall -g -fPIC -c f.c -o f.o and then gcc -shared -o libf.so f.o. To put it in the the jar I do java -jar ~/src/java/JNAerator/jnaerator/target/jnaerator-0.13-SNAPSHOT-shaded.jar -runtime BridJ -mode Jar f.c libf.so. Gcc version is 5.1.0. The Scala code I'm calling the function from is:

package test

object Test {
  def main(args: Array[String]): Unit = {
    import functions._
    import org.bridj.Pointer._
    val s = (new S).a(1).b(2.0)
    println(s.a)
    println(s.b)
    println()
    val p = pointerTo(s)
    println(p.get.a)
    println(p.get.b)
    println()
    val q = FunctionsLibrary.printHello(p)
    println(q.get.a)
    println(q.get.b)
  }
}

The error is raised by line println(q.get.a). If I change the code to print the values in s it doesn't print them for some reason. Also, if I return the int or double in s, I get random values.

Cheers, Ricardo