# How wrapp strcpy libc

**URL:** <https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471>\
**Category:** New to Julia\
**Created:** [June 26, 2017, 2:41pm UTC](https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471 "2017-06-26T14:41:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlxG78](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@AlxG78](https://discourse.julialang.org/u/AlxG78)\
**Post date:** [June 26, 2017, 2:41pm UTC](https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471/1 "2017-06-26T14:41:14Z")

</div>

How wrapp strcpy of libc which ccall, this does not work for me

```julia
to=Ref{Cstring}()
ccall(:strcpy, Ptr{UInt8}, (Ref{Cstring}, Ptr{UInt8}), to, "Hello")
unsafe_string(to)

```

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 26, 2017, 5:18pm UTC](https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471/2 "2017-06-26T17:18:37Z")

</div>

The issue with your example is that you are not allocating any memory for the destination pointer. `Ref{Cstring}()` is an empty string. Technically what you want to do is possible in Julia, but it is risky even for expert C developers. See `pointer` or `Libc.malloc` depending on whether the memory needs to be owned by Julia. Please carefully read the “Calling C” section of the manual, especially this and the following section:

[https://docs.julialang.org/en/latest/manual/calling-c-and-fortran-code/#Memory-Ownership-1](https://docs.julialang.org/en/latest/manual/calling-c-and-fortran-code/#Memory-Ownership-1)

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [June 26, 2017, 7:37pm UTC](https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471/3 "2017-06-26T19:37:29Z")

</div>

Maybe you could explain what you’re trying to achieve? There’s probably an easier way of doing it.

---

<div class="post-metadata">

**Author:** ![AlxG78](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@AlxG78](https://discourse.julialang.org/u/AlxG78)\
**Post date:** [June 27, 2017, 4:51am UTC](https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471/4 "2017-06-27T04:51:03Z")

</div>

Just to learn, this works for me

```julia
function test(s)
    to = Ptr{UInt8}(Libc.malloc(length(s)*sizeof(Char)))
    ccall(:strcpy, Ptr{UInt8}, (Ptr{UInt8}, Ptr{UInt8}), to, s)
    str = unsafe_string(to)
    Libc.free(to)
    str
end

julia> test("Hello World!")

```

Thank you

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 27, 2017, 4:55am UTC](https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471/5 "2017-06-27T04:55:01Z")

</div>

Why would you want to do this?

---

<div class="post-metadata">

**Author:** ![AlxG78](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@AlxG78](https://discourse.julialang.org/u/AlxG78)\
**Post date:** [June 27, 2017, 5:22am UTC](https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471/6 "2017-06-27T05:22:13Z")

</div>

It’s not at all I’m just trying to learn how to use ccall, with examples of libc

Sorry for my English, I’m Spanish

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [June 27, 2017, 6:38am UTC](https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471/7 "2017-06-27T06:38:47Z")

</div>

Is there any downsides to using an array to allocate memory instead of malloc ?

```julia
to = Array{UInt8}(sizeof(s))

```

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [June 28, 2017, 6:39am UTC](https://discourse.julialang.org/t/how-wrapp-strcpy-libc/4471/8 "2017-06-28T06:39:54Z")

</div>

> [@AlxG78](#):
>
> Just to learn, this works for me

It’s unclear what you’re trying to do, but this code is wrong; I’ve added comments inline to indicate possible corrections:

```julia
function test(s)
    to = Ptr{UInt8}(Libc.malloc(length(s)*sizeof(Char)))
      # should allocate `sizeof(s)` bytes
    ccall(:strcpy, Ptr{UInt8}, (Ptr{UInt8}, Ptr{UInt8}), to, s)
      # the use of strcpy in C should be avoided, use memmove instea
      # if you must deal with a poorly-designed C-api that uses `char*` without a size/length argument,
      # be sure to mark the types as `Cstring` in the `ccall` so they are null-terminated by Julia
    str = unsafe_string(to)
      # could just replace this function with `return s` :P 
    Libc.free(to)
      # its often recommended to wrap the usage in try/finally
      # so that error don't leak memory (or use Array allocation as suggested by jonathanBieler)
    return str
end

```
