# Help translating Cython to Julia

**URL:** <https://discourse.julialang.org/t/help-translating-cython-to-julia/12262>\
**Category:** General Usage\
**Tags:** memory-allocation\
**Created:** [July 9, 2018, 12:01am UTC](https://discourse.julialang.org/t/help-translating-cython-to-julia/12262 "2018-07-09T00:01:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [July 9, 2018, 12:01am UTC](https://discourse.julialang.org/t/help-translating-cython-to-julia/12262/1 "2018-07-09T00:01:41Z")

</div>

(Sorry for all the different venues for this, I’ve had this conversation piecewise with many of you already)

I’m trying to write the equivalent julia code for the following Cython code:

[https://github.com/mapd/pymapd/blob/01019c6a48814a44644499bf4053f46e488e7ac6/pymapd/shm.pyx#L31-L52](https://github.com/mapd/pymapd/blob/01019c6a48814a44644499bf4053f46e488e7ac6/pymapd/shm.pyx#L31-L52)

I receive the following from calling an external API, where I’m trying to IPC using shared memory:

```julia
julia> tdf.sm_handle
4-element Array{UInt8,1}:
 0x9b
 0x76
 0x9a
 0x18

julia> tdf.sm_size
93856

```

I’ve gotten this far:

```julia
shmkey = reinterpret(UInt32, tdf.sm_handle)[1]
shmid = ccall((:shmget, "libc"), Cint, (Cuint, Int32, Int32), shmkey, tdf.sm_size, 0)
ptr = ccall((:shmat, "libc"), Ptr{Void}, (Cint, Ptr{Void}, Cint), shmid, C_NULL, 0)

julia> ptr
Ptr{Void} @0x00007f67fc7b4000

```

I believe the code above is equivalent to the Cython code, but I’m not quite sure. Would take any help there. But ultimately, my question is, if `ptr` is actually the location of the first element of the array of shared memory, how do I bring all of the data from shared memory into Julia?

When I debug the Cython code, `pabuff` is intermingled with ASCII text I recognize, along with a bunch of `0x00` and the like in between all the text. But not having any real experience with C and shared memory, I’m stuck between how I take the shared memory ID I’m passed and actually read the data into julia.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [July 9, 2018, 2:57am UTC](https://discourse.julialang.org/t/help-translating-cython-to-julia/12262/2 "2018-07-09T02:57:43Z")

</div>

Disclaimer: I haven’t used `shmat` in real life; I just read the man page.

It looks like you’ve got the pointer to the array as expected. In you cython code, you cast that to a pointer to `uint8` and then wrap that as an array. You can do the same in Julia:

```julia
julia> unsafe_wrap(Array, convert(Ptr{UInt8}, ptr), sm_size)
100-element Array{UInt8,1}:
<your data here>

```

Note that this Julia array will reference the same data as the attached shared memory. If you’re going to free that shared memory, you should `copy()` the resulting array to avoid referencing garbage.

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [July 9, 2018, 12:37pm UTC](https://discourse.julialang.org/t/help-translating-cython-to-julia/12262/3 "2018-07-09T12:37:21Z")

</div>

Thanks so much @rdeits, it was `convert(Ptr{UInt8}, ptr)` that I was missing. I tried `unsafe_wrap`, but with `Ptr{Void}`, I kept getting a Julia error.
