# ccall, C pointers and Multi-Core 

**URL:** <https://discourse.julialang.org/t/ccall-c-pointers-and-multi-core/16733>\
**Category:** Julia at Scale\
**Created:** [October 24, 2018, 12:29pm UTC](https://discourse.julialang.org/t/ccall-c-pointers-and-multi-core/16733 "2018-10-24T12:29:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mischmi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mischmi/32/9787_2.png) [@mischmi](https://discourse.julialang.org/u/mischmi)\
**Post date:** [October 24, 2018, 12:29pm UTC](https://discourse.julialang.org/t/ccall-c-pointers-and-multi-core/16733/1 "2018-10-24T12:29:05Z")

</div>

Hello,

I’m having a bit of trouble with parallelization and a C library. I’ll try to describe the issue as best as possible:

I have a certain mutable struct that stores pointers to memory allocated by C through a ccall. I can use certain functions on the struct that again call a C library and modify the pointers and so on.

Now, I initialize the struct (call it p) with @spawnat. I apply a function f to the struct on those workers with @spawnat N f(p). (This function can take a few seconds and sets the pointer of a field [let’s call it F] from undef to memory allocated by C.) If I do this in the REPL manually for a certain worker then everything works just fine and as intended.

The problem occurs if try to generalize this. I define a wrapper function that initializes the struct on every worker on a variable called p. Then I have a second function that applies the function f from above to the variable p on every worker. This variable does exist, but the C pointers in the struct seem to be broken.

additional Information: I don’t return the pointer when someone tries to access the field but apply unsafe\_wrap beforehand and return the result.

I receive this error when trying fetch(@spawnat 5 p.F):

```julia
 Error: Fatal error on process 5
│ exception =
│ IOError: write: bad address in system call argument (EFAULT)
│ Stacktrace:
│ [1] try_yieldto(::typeof(Base.ensure_rescheduled), ::Base.RefValue{Task}) at ./event.jl:196

```

Has someone an idea what may cause this behavior?

---

<div class="post-metadata">

**Author:** ![mischmi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mischmi/32/9787_2.png) [@mischmi](https://discourse.julialang.org/u/mischmi)\
**Post date:** [October 24, 2018, 4:32pm UTC](https://discourse.julialang.org/t/ccall-c-pointers-and-multi-core/16733/2 "2018-10-24T16:32:55Z")

</div>

Okay, i figured it out. If I have 10 workers they are indexed from 2 to 11 and not 1 to 10. This messed up the scope of the variables.
