Ah, thanks for the great explanation! -s
On Sat, Dec 21, 2013 at 8:46 PM, Isaiah Norton <[email protected]>wrote: > Would it also work if instead of instead of declaring a 1-element array >> with Cint[0], you just declared an actual Cint with convert(Cint, 0) (or >> some other mechanism for creating an int)? Or would that not be the same >> thing because Cint is supposed to be immutable? >> > > No. In C, it is possible to take the address of a stack variable and this > address will be valid as long as the stack frame exists. So one can pass > the address to a function call, and when the function returns, any change > in value is visible in the calling frame. > > In a Julia ccall, the "&" operator causes the value of the referenced > variable to be copied into a temporary space, and the compiler passes the > address of the *temporary* space. When the function returns, the temporary > space is destroyed, and any changes are invisible to the calling Julia > block. > > > On Saturday, December 21, 2013, Spencer Russell wrote: > >> Sounds good, writing it up now. >> >> One thing: >> >> Would it also work if instead of instead of declaring a 1-element array >> with Cint[0], you just declared an actual Cint with convert(Cint, 0) (or >> some other mechanism for creating an int)? Or would that not be the same >> thing because Cint is supposed to be immutable? >> >> -s >> >> >> On Sat, Dec 21, 2013 at 3:21 PM, Tim Holy <[email protected]> wrote: >> >>> On Saturday, December 21, 2013 02:28:01 PM Spencer Russell wrote: >>> > Ah, thanks for this! Just yesterday I was trying to figure out how to >>> pass >>> > some pointers so they could be modified by a C function. This is a >>> pretty >>> > common idiom in lots of C code, maybe this little trick could be added >>> to >>> > the docs on C interop? >>> >>> Pull requests are always welcome, and there are advantages to having >>> people >>> with "fresh eyes" contributing to the documentation. >>> >>> For documentation, you don't even need to mess with git; you can edit >>> right on >>> the webpage. >>> >>> Best, >>> --Tim >>> >>> > >>> > -s >>> > >>> > On Sat, Dec 21, 2013 at 10:31 AM, Isaiah Norton >>> <[email protected]>wrote: >>> > > ``` >>> > > >>> > > width = Cint[0] >>> > > >>> > > range = Cfloat[0] >>> > > >>> > > ccall(:foo, Void, (Ptr{Cint}, Ptr{Cfloat}), width, range) >>> > > >>> > > ``` >>> > > >>> > > >>> > > Explanation: >>> > > >>> > > - the first line declares an array with a single element of Cint >>> (Int32) >>> > > type >>> > > >>> > > - ccall automatically passes the address of the first (here, only) >>> element >>> > > of the array when you request Ptr{Cint} / Ptr{Cfloat} in the type >>> > > specification tuple> >>> > > On Sat, Dec 21, 2013 at 10:15 AM, Andreas Lobinger >>> <[email protected]>wrote: >>> > >> Hello, >>> > >> >>> > >> i'm trying to use an external library for certain things and i need >>> > >> somehow a 1:1 relation to certain input/output parameters: >>> > >> >>> > >> *float >>> > >> *int >>> > >> >>> > >> I'm aware that the most promising thing is to do a type definition. >>> > >> >>> > >> Is >>> > >> >>> > >> type myiothing >>> > >> >>> > >> input::Array{Float32,1} >>> > >> output::Array{Int32,1} >>> > >> n_points:Int32 >>> > >> >>> > >> end >>> > >> >>> > >> the way to go, or is there something i'm missing (i have no clear >>> > >> understanding, what Array{something,1} is creating only a pointer)? >>> > >> >>> > >> Wishing a happy day, >>> > >> >>> > >> Andreas >>> >> >>
