xlab / c-for-go

Automatic C-Go Bindings Generator for Go Programming Language
https://c.for-go.com
MIT License
1.5k stars 119 forks source link

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

Closed nivanov-ati closed 5 months ago

nivanov-ati commented 8 months ago

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!