This is absolutely possible. You can take any pointer and turn it into an array. The only concern is garbage collection and making sure that Julia knows whether it should free the memory or not.
> On Aug 10, 2014, at 3:31 PM, Gerry Weaver <[email protected]> wrote: > > Hi Kevin, > > It probably would, but in this case the C function takes a void pointer > argument and passes that to the Julia callback function. Unfortunately, I > can't change the type of the argument. I was hoping that there was a way to > convert that void pointer back to an array, but it isn't looking like that is > possible. I guess the answer is to pass a blob of text with some type of > delimiter to pass multiple values. That would be significantly slower though. > > Thanks, > -G > >> On Sunday, August 10, 2014 10:56:23 AM UTC-5, Kevin Squire wrote: >> Hi Gerry, >> >>> I think I see whats going on. If the pointer that is being passed to >>> pointer_to_array is void, then you get a array of void pointers. In my case >>> the original array was an array of strings, so... >>> >>> arr = pointer_to_array(cbdata, 4, true) >>> println(bytestring(convert(Ptr{Uint8}, val[1]))) >> >> Was the original array of strings allocated by Julia, or in your C function? >> >> I think your solution is close. It would be slightly cleaner if you >> declared you function to take a Ptr{Ptr{Uint8}}: >> >> function my_callback(cbdata::Ptr{Ptr{Uint8}}) >> arr = pointer_to_array(cbdata, 4) # arr of length 4 >> println(bytestring(arr[1])) >> nothing >> end >> >> cbfunc = cfunction(my_callback, Void, (Ptr{Ptr{Uint8}},)) >> >> Does this work? >> >> BTW, have you seen Steven Johnson's blog post on Passing Julia Callback >> Functions to C? I've found it quite useful. >> >> Cheers! >> Kevin >> >> >> >>> >>> Is there a better way to do this? >>> >>> Thanks, >>> -G >>> >>> >>>> On Sunday, August 10, 2014 4:23:29 AM UTC-5, Gerry Weaver wrote: >>>> Hi Kevin, >>>> >>>> I found the pointer_to_array function, but I'm having some trouble >>>> figuring how to call it correctly. I assume it has something to do with >>>> the pointer being a void pointer. I've been searching for something that >>>> might give me a clue how to do this, but I haven't found anything very >>>> useful so far. >>>> >>>> Thanks, >>>> -G >>>> >>>>> On Sunday, August 10, 2014 4:06:52 AM UTC-5, Kevin Squire wrote: >>>>> Hi Gerry, >>>>> >>>>> "pointer_to_array" is probably what you're looking for. See >>>>> http://julia.readthedocs.org/en/latest/manual/calling-c-and-fortran-code/#accessing-data-through-a-pointer >>>>> for more information. >>>>> >>>>> Cheers, >>>>> Kevin >>>>> >>>>> >>>>>> On Sun, Aug 10, 2014 at 1:17 AM, Gerry Weaver <[email protected]> wrote: >>>>>> I forgot to mention that I'm trying to accomplish this with arrays right >>>>>> now. I can pass a Julia array to C and access it there, but I can't >>>>>> figure out how to convert it back to a Julia array from a C void pointer >>>>>> when calling back to Julia from C. >>>>>> >>>>>> >>>>>>> On Sunday, August 10, 2014 1:57:18 AM UTC-5, Gerry Weaver wrote: >>>>>>> Hello All, >>>>>>> >>>>>>> I have a C function called mycfunc that takes a callback function >>>>>>> pointer (cbfunc) and a void pointer (cbdata) as arguments. mycfunc >>>>>>> calls the callback function (cbfunc) passing the void pointer (cbdata) >>>>>>> as its argument. I'm trying to figure out how to pass a Julia type as >>>>>>> the void pointer argument and convert it back to the Julia type in the >>>>>>> Julia function passed as the callback function argument. I can do this >>>>>>> with a simple string, but I'm having some difficulty in figuring out >>>>>>> how to do it with arrays and other types. Any advice on this would be >>>>>>> much appreciated. >>>>>>> >>>>>>> >>>>>>> typedef int (* mycallback)(void *cbdata); >>>>>>> >>>>>>> void mycfunc(mycallback cbfunc, void *cbdata) >>>>>>> { >>>>>>> cbfunc(cbdata); >>>>>>> } >>>>>>> >>>>>>> Thanks, >>>>>>> -G >>
