Thanks Jameson, I want to later use `x` in auto-generated expressions, and if it's a Ref I will have to use `x.x` to get the actual data which makes generating the expressions more tricky, I thought I could perhaps avoid it.
Here's a more complete snippet: ~~~ type Foo x::Int64 y::Vector{Int64} end Foo() = Foo(0, zeros(Int64, 10)) bar = :(f.x + f.y[2]) expr = :( f = Foo() for i=1:10 ccall((:myfunc_x, lib), Void, (Ptr{Void}, ), pointer(h.x)) #fill the scalar f.x ccall((:myfunc_y, lib), Void, (Ptr{Void}, ), pointer(h.y)) #fill the array f.y r = $(eval(bar)) #do something with f.x and f.y end #done with f, OK to clean up ) ~~~ Does this seem like an OK approach with Refs and is there a way to avoid needing to do `h.x.x` in the expressions? On Tuesday, 1 September 2015 23:51:34 UTC+2, Jameson wrote: > > use `Ref{Clong}` for the calling convention to pass an auto-allocated > boxed c-long. > > it's usually best to avoid `pointer` and `pointer_from_objref`. memory is > not "guaranteed by julia": it will be cleaned up as soon as the gc detects > that you are no longer referencing the julia object. using `pointer` and > `pointer_from_objref` hides memory from the gc, making it easier for you to > create memory bugs in your program. > > > On Tue, Sep 1, 2015 at 5:46 PM Joosep Pata <joose...@gmail.com > <javascript:>> wrote: > >> I want to modify the data stored in an elementary julia object (e.g. >> Int64) using external C code. I can do this if the type is wrapped in an >> array, but is it possible to do something like `pointer(x::Float64) -> >> address of data in x` with the memory being guaranteed by julia? >> >> Here’s a short snippet to describe: >> ~~~ >> >> #C side >> void myfunc(long* x) { >> x[0] += 1; >> } >> >> #julia side, works >> x = Int64[0] >> ccall((:myfunc, lib), Void, (Ptr{Void}, ), convert(Ptr{Void}, pointer(x))) >> println(x) >> >> [1] >> >> #julia side, desired >> x = 0 >> ccall((:myfunc, lib), Void, (Ptr{Void}, ), pointer(x)) >> println(x) >> >> 1 >> ~~~ >> >> I tried pointer_from_objref but as I understand this gives me the pointer >> of the whole julia type with meta-data etc. If I write to this address in >> C, I fear I’ll have problems. > >