Hacker News new | ask | show | jobs
by celrod 1459 days ago
I find `ccall` and especially `@ccall` easy to use, but thankfully haven't spent much time wrapping C libraries so I'd consider myself far from an expert in the matter.

Creating mutable structs and `GC.@preserve`ing them is an effective means of getting stack allocated memory, so long as the structs do not escape.

E.g., I occasionally follow this approach:

  mem = Ref{NTuple{32,Float64}}()
  GC.@preserve mem begin
    p = Base.unsafe_convert(Ptr{Float64}, mem)
    # do things with p, e.g. pass it to a C library
  end