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 <joosep.p...@gmail.com> 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.

Reply via email to