pointer_from_objref is mentioned as a parenthetical, and actually I'm not sure if it needs to be mentioned at all in the manual because this usage is covered by specifying a type of `Any` to ccall. I agree it could be better documented in the stdlib docs.
On Sun, Jan 18, 2015 at 11:36 AM, Milan Bouchet-Valat <nalimi...@club.fr> wrote: > Le dimanche 18 janvier 2015 à 10:15 -0500, Isaiah Norton a écrit : > > 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) > I think this deserves to be in the manual, and in relevant documentation > for functions in stdlib. ?pointer_from_objref and ?pointer do not really > allow grasping the difference between the two functions. > > > Regards > > > 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 > > > > > > > > > >