# Help with ccall to a clib function

**URL:** <https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747>\
**Category:** General Usage\
**Created:** [September 6, 2017, 6:23pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747 "2017-09-06T18:23:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Frank\_Applin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frank_applin/32/1654_2.png) [@Frank\_Applin](https://discourse.julialang.org/u/Frank_Applin)\
**Post date:** [September 6, 2017, 6:23pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747/1 "2017-09-06T18:23:56Z")

</div>

Hi,

I’m trying to implement some routines from termios in clib. I have created the constants to represent the many #defines in the c header file, but I need help with a couple items (please). So, here’s a bit of the code:

```julia
@ctypedef tcflag_t Culong
@ctypedef cc_t Cuchar
@ctypedef speed_t Clong

struct termios
    c_iflag::tcflag_t	#input flags
    c_oflag::tcflag_t	#output flags
    c_cflag::tcflag_t	#control flags
    c_lflag::tcflag_t	#local flags
    c_cc::Ptr{cc_t} #control chars #this is what it looks like in C - cc_t c_cc[NCCS]; 
    c_ispeed::speed_t	#input speed
    c_ospeed::speed_t	#output speed 
end

```

So, the first question is - am I translating an array of integers correctly in the struct?

The second question is - how do I initialize the struct (since it has an array in it)?

The C code looks like this:

```julia
struct termios tty;
memset(&tty, 0, sizeof(tty));

```

this would be followed by:

```julia
if (tcgetattrib(fd, &tty) != 0)
{
    printf("error from tcgetattrib");
    return -1;
}

```

I translated that to:

```julia
tty = termios(0,0,0,0,0,0,0) #I think the initialization is wrong

ret = ccall((:tcgetattr, "libc"), Int32, (Int32, Ref{termios}), fd, tty)

if (ret != 0)
   println("error from tcgetattr")
   return -1
end

```

Can someone help me along, here?

Frank

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [September 6, 2017, 8:34pm UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747/2 "2017-09-06T20:34:26Z")

</div>

(could you please edit your post and put tripple backquotes ``` around the code parts?)

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [September 7, 2017, 1:58am UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747/3 "2017-09-07T01:58:32Z")

</div>

I think this is the correct way to handle this (not tested):

```nohighlight
array = zeros(cc_t, N)
ptr = Base.unsafe_convert(Ptr{cc_t}, array ) # unsafe! make sure to keep a reference to array alive
tty = termios(0,0,0,0,ptr ,0,0)
# C argument types need to be Ptr (the type closest to C), while Ref should be passed as an argument mirroring Ptr in C.
ret = ccall((:tcgetattr, "libc"), Int32, (Int32, Ptr{termios}), fd, Ref(tty))

```

Array pointers in structs are not that nicely documented ☹

---

<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:** [September 7, 2017, 2:32am UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747/4 "2017-09-07T02:32:38Z")

</div>

> [@sdanisch](#):
>
> ptr = Base.unsafe\_convert(Ptr{cc\_t}, array ) # unsafe! make sure to keep a reference to array alive

Basically don’t call `unsafe_convert` manually like this. The code here is allowed to crash.

Also this appears to be duplicated at [Help with ccall to clib functions](https://discourse.julialang.org/t/help-with-ccall-to-clib-functions/5749) Note that this is not the correct way to declare inline array member (and note that C arrays are **NOT** pointer).

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [September 7, 2017, 2:44am UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747/5 "2017-09-07T02:44:55Z")

</div>

Oh, I didn’t see the full comment about c\_cc being `c_cc[NCCS]`

> Basically don’t call unsafe\_convert manually like this. The code here is allowed to crash.

Can you remind me what the correct way is, in case it’s not a statically sized array in C, but indeed a pointer?

---

<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:** [September 7, 2017, 2:51am UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747/6 "2017-09-07T02:51:05Z")

</div>

> [@sdanisch](#):
>
> in case it’s not a statically sized array in C, but indeed a pointer?

You should call `Base.cconvert` first and call `Base.unsafe_convert` on top of that. (Or call `pointer` on the array).  
Also, you have to make sure that the result of the `cconvert` is live throughout the time you need to use the pointer.  
There isn’t a well defined way to do this on 0.6 though depending on the context there are a few way to do it taking advantage of the optimizer deficiency ([store the reference to a type](https://github.com/JuliaPy/PyCall.jl/blob/2e5ff6f91a1c35b2651e84335a891c611c2d7e3c/src/pytype.jl#L314) and makes sure that wrapper type is live until the pointer is not needed works most of the time). As most of the optimizer issues are being fixed on 0.7 [Fancier GC preserve intrinsics by Keno · Pull Request #23610 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/23610) will be the way to do it.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [September 7, 2017, 2:56am UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747/7 "2017-09-07T02:56:05Z")

</div>

Thanks! Sorry for further derailing but I’m always tripping over this, so I want to make sure I understand it this time. Is this considered to be correct:

```nohighlight
immutable JLTest
x::Vector{Cint}
end

immutable CTest
x::Ptr{Cint}
y::Any
end

function cconvert(::Type{CTest}, x::JLTest)
     CTest(pointer(x.x), x.x)
end
ccall(:mutate_in_c, Void, (Ctest,), x::JLTest)

```

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [September 7, 2017, 3:07am UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747/8 "2017-09-07T03:07:01Z")

</div>

@yuyichao feel free to ignore this, on a second look your examples are pretty clear!

---

<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:** [September 7, 2017, 3:21am UTC](https://discourse.julialang.org/t/help-with-ccall-to-a-clib-function/5747/9 "2017-09-07T03:21:48Z")

</div>

The code is valid though

1. I think the `ccall` should use `CTest` instead of `Ctest`
2. Unless the C code actually want a `struct { int *; void *; };` as input parameter (passed by value) you might need to change how you write the `ccall`. (The julia code you have is correct, just that the C API usually doesn’t look like this)

Other examples of valid `cconvert` def include `CTest(pointer(x.x)), x.x` or `y = cconvert(Ptr{Cint}, x.x); CText(unsafe_convert(Ptr{Cint}, y), y)` It is not necessary to store the reference in the struct for this case since it’s in `cconvert` and you are only dealing with `ccall`. This is the only case we guarantee on 0.4-0.6 about the rooting lifetime. The example I give is only necessary if you are to keep the converted result for more than a `ccall` or when you aren’t/can’t pass the object to be rooted to the `ccall`.
