# Ccall with pointer argument to array of custom type via an NTuple of immutables

**URL:** https://discourse.julialang.org/t/ccall-with-pointer-argument-to-array-of-custom-type-via-an-ntuple-of-immutables/1048
**Category:** General Usage
**Tags:** ccall
**Created:** [December 19, 2016, 8:59pm UTC](https://discourse.julialang.org/t/ccall-with-pointer-argument-to-array-of-custom-type-via-an-ntuple-of-immutables/1048 "2016-12-19T20:59:48Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [December 19, 2016, 9:20pm UTC](https://discourse.julialang.org/t/ccall-with-pointer-argument-to-array-of-custom-type-via-an-ntuple-of-immutables/1048/2 "2016-12-19T21:20:41Z")

</div>

> [@m-j-w](#):
>
> rarg = Ref{MyType} # \<---- (A)

This is wrong, you at least need `Ref{MyType}()`

> [@m-j-w](#):
>
> CString

`Cstring`

> [@m-j-w](#):
>
> pointer\_from\_objref(wrapped\_args)

Never call `pointer_from_objref` period.

Use `ccall(..., (..., Ptr{Void}, Ref{MyType}, ...), ..., Ref(wrapped_args), rarg, ...)` instead.

> [@m-j-w](#):
>
> How to best create a reference to MyType which should also be default initialized? Here Ref appears to create unintialized memory.

`Ref(MyType())`

> [@m-j-w](#):
>
> (B,C): How can I get to the correctly GC managed Ref{MyType} or similar when having such an NTuple of MyTypes?

You don’t.

> [@m-j-w](#):
>
> I figured I need to write a Base.cconvert and Base.unsafe\_convert method

You shouldn’t define it for `NTuple`

To make the syntax more consistent we could define in base

```julia
Base.cconvert{N,T}(::Type{Ref{T}}, t::NTuple{N,T}) = Ref{NTuple{N,T}}(t)
Base.unsafe_convert{N,T}(::Type{Ref{T}}, r::Ref{NTuple{N,T}}) = unsafe_convert(Ref{NTuple{N,T}}, r)

```

---

_[View the full topic](https://discourse.julialang.org/t/ccall-with-pointer-argument-to-array-of-custom-type-via-an-ntuple-of-immutables/1048)._
