GithubHelp home page GithubHelp logo

Return a struct by value about jnr-ffi HOT 8 OPEN

jnr avatar jnr commented on September 28, 2024 4
Return a struct by value

from jnr-ffi.

Comments (8)

juraj-bicikl avatar juraj-bicikl commented on September 28, 2024 1

I have encountered the same issue as @jeromerobert when trying to connect to a C library. For example, consider the following C code:

struct IntPair {
    int a;
    int b;
};

// some function which creates a pair
struct IntPair makePair(int x, int y) {

    struct IntPair p;
    p.a = x;
    p.b = y;

    return p;
}

Here makePair returns IntPair struct itself, and not just a pointer to it.
Is there a way to bind this function in jnr-ffi?
In all the test examples in the jnr-ffi project, C functions return struct pointers, and not structs.

from jnr-ffi.

headius avatar headius commented on September 28, 2024

Care to put that in terms of an example test case?

Note that there's been improvements in how Struct is laid out and passed around, so check 2.1 out.

from jnr-ffi.

jeromerobert avatar jeromerobert commented on September 28, 2024

I modified the getpid example as if pid_t was:

struct pid_t { int value; }

from jnr-ffi.

sgnsajgon avatar sgnsajgon commented on September 28, 2024

Is it possible to return a struct by value in version 2.19?

I had asked question on Stack Overflow before I found this GitHub issue:

https://stackoverflow.com/questions/56129984/how-to-return-by-value-from-native-function

How can I prevent memory leak shown in SO question?

from jnr-ffi.

charleskorn avatar charleskorn commented on September 28, 2024

This appears to still be an issue - is using a function that returns a struct by value supported?

from jnr-ffi.

basshelal avatar basshelal commented on September 28, 2024

@charleskorn yeah this is still an issue, see #262
Unfortunately it's also quite low priority I'd say, unless you have access to the original library code and don't mind recompiling and using a struct by reference (or pointer) function then you'll have better luck with JNA, which I believe does support structs passed and returned by value, but I might be wrong about that so do check just in case.

from jnr-ffi.

charleskorn avatar charleskorn commented on September 28, 2024

Makes sense, thanks for the quick response @basshelal. If it helps raise the priority, this makes working with functions exported from Golang's cgo quite difficult - in cgo, any function that returns multiple values (a very common pattern in Golang) gets translated to a function that returns a struct by value.

from jnr-ffi.

basshelal avatar basshelal commented on September 28, 2024

Thanks @charleskorn
Yeah it's annoying that struct by value is more common than I'd like and we never had support for it.

Personally I'd want it to be higher priority since I've had to deal with my own problems with struct by value as a parameter but the fix would be quite complicated to implement and verify, hence the lower priority.

But you are welcome to look into the code yourself and see if you can help with anything, you can also join our matrix discussion:
https://matrix.to/#/#jruby:matrix.org

from jnr-ffi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.