Hi all, I am trying to get data over to C, and I went with a ccall that takes in a buffer from julia. The issue I am having is, the data at teh end of my struct is getting chopped off.
Now if I just pass the variables as normal (Int64, Bool, Cdouble, etc) the data comes out just fine. Also, I commented out the bool, and looked at the data, and there was some non-zero data at the end. Does anyone know what is going on here?
It’s hard to tell without a runnable example & the function signature on the C side you’re trying to call. Can you provide a fully-formed MWE that others can run on their machine, to help us help you?
You’re probably observing padding in the struct. You don’t generally need to reinterpret anything when using ccall, provided the structs in C & Julia match.
Ok, I think I see the issue; there’s a few problems/misconceptions here.
Julia structs match the layout of C structs, so there’s no need to go through uint8_t*. You can go through CntlOutput* just fine, without having to memcpy things around.
You can then call sendUDPbytes by passing a foo = Ref(blah) like @ccall "./udpSend.so".sendUDPbytes(foo::Ptr{CntlOutput}, 1::Cint). If your intention is to pass a collection/an array of values to C, you can just pass that too (see these C wrapper examples from the manual).
EDIT: you generally don’t need to define that either, unless you have a fancier wrapper struct that doesn’t match the layout on the C side anymore (e.g. it contains Arrays where C would have pointers… in those cases, you’ll want to have a “julia version” that you then cconvert to a different struct matching the C version).
If you use the ccall version, you still need to give it the argument types seperately from the objects themselves. Only the @ccall macro can mix them like that. See also their respective docstrings: