# How can you modify global variables that are used in functions that are being called with ccall?

**URL:** <https://discourse.julialang.org/t/how-can-you-modify-global-variables-that-are-used-in-functions-that-are-being-called-with-ccall/93644>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 27, 2023, 4:21pm UTC](https://discourse.julialang.org/t/how-can-you-modify-global-variables-that-are-used-in-functions-that-are-being-called-with-ccall/93644 "2023-01-27T16:21:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmmpie](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dmmpie](https://discourse.julialang.org/u/dmmpie)\
**Post date:** [January 27, 2023, 4:21pm UTC](https://discourse.julialang.org/t/how-can-you-modify-global-variables-that-are-used-in-functions-that-are-being-called-with-ccall/93644/1 "2023-01-27T16:21:54Z")

</div>

From the [documentation](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Accessing-Global-Variables), I got that it’s possible to retrieve pointers to global variables by using the `cglobal` function. However, I wasn’t able to figure out from that how I can set the value of a global variable that is being used in a function that I am calling via `ccall`, or how I can retrieve the resulting state of a global variable that was modified by said function. Is there a part of the documentation I missed, or some kind of more in-depth tutorial on how to use `ccall` available anywhere?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 27, 2023, 4:53pm UTC](https://discourse.julialang.org/t/how-can-you-modify-global-variables-that-are-used-in-functions-that-are-being-called-with-ccall/93644/2 "2023-01-27T16:53:17Z")

</div>

Since `cglobal` returns a pointer, you’d use `unsafe_load`/`unsafe_store!` to read/write values to/from that global variable.

---

<div class="post-metadata">

**Author:** ![dmmpie](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@dmmpie](https://discourse.julialang.org/u/dmmpie)\
**Post date:** [January 27, 2023, 4:59pm UTC](https://discourse.julialang.org/t/how-can-you-modify-global-variables-that-are-used-in-functions-that-are-being-called-with-ccall/93644/3 "2023-01-27T16:59:00Z")

</div>

@Sukera But will the modifications I make also be available in the subsequent `ccall` call? I assumed that ccall loads a fresh copy of the binary into memory, so any modifications made beforehand won’t be adopted

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [January 27, 2023, 5:05pm UTC](https://discourse.julialang.org/t/how-can-you-modify-global-variables-that-are-used-in-functions-that-are-being-called-with-ccall/93644/4 "2023-01-27T17:05:34Z")

</div>

`ccall` doesn’t load libraries on its own. If the call succeeds, the library was already loaded. You’d use `Libdl` and `dlopen` to load a shared object (`*.so`) and the functions contained within. `ccall` does nothing more than locate that already located function, based on the function name you passed to it, and call it directly (i.e., push the arguments on the stack and jump to the code of the function).

Global variables in C are just some address in memory - they too have a name associated with them, and that’s what `cglobal` uses to return that address as a pointer.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 27, 2023, 5:33pm UTC](https://discourse.julialang.org/t/how-can-you-modify-global-variables-that-are-used-in-functions-that-are-being-called-with-ccall/93644/5 "2023-01-27T17:33:41Z")

</div>

You can `ccall` libraries you haven’t loaded beforehand just fine. But regardless `ccall` won’t close them after the call.
