Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing in an array to a C function and reading the data - incorrect autogenerated Go code #167

Open
nivanov-ati opened this issue Mar 20, 2024 · 0 comments

Comments

@nivanov-ati
Copy link

Hi! Thank you for your work on c-for-go. We're using it to generate go bindings for the uldaq library. Here's the function that gives us issues:

https://github.com/mccdaq/uldaq/blob/1d8404159c0fb6d2665461b80acca5bbef5c610a/examples/DInScan.c#L150

ulDInScan collects data from the data acquisition device and saves it into the provided buffer.

When we generate bindings for ulDInScan, the buffer "data" gets the following treatment in autogenned go code:

cdata, cdataAllocMap := copyPUlonglongBytes((*sliceHeader)(unsafe.Pointer(&data)))

...but the provided data array never gets populated

I searched for similar patterns and found one here: https://github.com/5k3105/nidaq/blob/d33879c3427eb8b59a08a65a41edd3ad00459e85/nidaqmx.go#L1482C19-L1482C113

and in this case, the provided array is simply passed along as a pointer.

I modified the autogenned go code for ulDInScan to mimic the link above:

cdata, cdataAllocMap := (*C.ulonglong)(unsafe.Pointer((*sliceHeader)(unsafe.Pointer(&data)).Data)), cgoAllocsUnknown

...and it works great!

However, modifying autogenned code is obviously not the right approach. How can I tell c-for-go to not copy the buffer but rather just use the array pointer?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant