I should also emphasize the difference: pointer_from_objref returns a jl_value_t*, which includes the type tag found at the beginning of all (well, most) Julia value instances. With "Ptr{T}" and "&v" in ccall, you get a pointer to the memory after the type tag, which is C-compatible (provided isbits(T) == true)
On Sun, Jan 18, 2015 at 10:12 AM, Isaiah Norton <isaiah.nor...@gmail.com> wrote: > - if you want to pass a pointer to the *complete* Julia type, including > the type tag, use pointer_from_objref. From the manual: > > Julia values v can be converted to jl_value_t*pointers, as Ptr{Void}, by >> calling pointer_from_objref(v). > > > - if you want to pass a pointer to a C-compatible struct, tell ccall that > is what you want, and use the "&" symbol before the variable. From the > manual: > > struct T* (where T represents an appropriately defined bits type)Ptr{T} (call >> using &variable_name in the parameter list) > > Here is an example: > > https://gist.github.com/ihnorton/1c1338a22fb3d143fa7f > > Really the best way to work through these things is to write small > examples where you can control everything, and see what happens. Trying to > figure it out on the go in a large system with many moving pieces is a > recipe for frustration. > > On Sun, Jan 18, 2015 at 9:28 AM, J Luis <jmfl...@gmail.com> wrote: > >> Thank you very much Jiahao. That's indeed the result I want >> >> ... but sorry, I'm still confused. Why doesn't the other way work too? At >> the end it also gets composite type with the same members but with a >> whatever in their values. >> >> Also, I have here a case where it worked with unsafe_load() but crashes >> if I use unsafe_pointer_to_objref() (note that 'image' is a pointer to a >> composite type) >> >> https://github.com/joa-quim/IUP.jl/blob/master/examples/im_view_.jl#L106 >> >> I read and reread the docs but find confusing what is a *memory address >> of a Julia object* is that apparently is different to the pointer >> obtained with convert() >> >> >> domingo, 18 de Janeiro de 2015 às 06:17:11 UTC, Jiahao Chen escreveu: >> >>> I don't think the conversion does what you want. Instead you may be >>> looking for this: >>> >>> julia> type foo a::Int; b::Int end >>> >>> julia> tp=foo(2,3) >>> foo(2,3) >>> >>> julia> p=pointer_from_objref(tp) >>> Ptr{Void} @0x00007fa70872e188 >>> >>> julia> pt=unsafe_pointer_to_objref(p) >>> foo(2,3) >>> >>> Thanks, >>> >>> Jiahao Chen >>> Staff Research Scientist >>> MIT Computer Science and Artificial Intelligence Laboratory >>> >> >