# Does \`GC.@preserve a b\` preserves the location of \`a\` and \`b\`

**URL:** https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233
**Category:** General Usage
**Created:** [May 27, 2020, 4:43am UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233 "2020-05-27T04:43:56Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [May 27, 2020, 4:43am UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/1 "2020-05-27T04:43:56Z")

</div>

I am using `GC.@preserve` in my code as follows

```julia
GC.@preserve missing_bytes res begin
    src_ptr = Ptr{UInt8}(pointer(missing_bytes))
    dest_ptr = Ptr{UInt8}(pointer(res, res_len+1)) + from - 1
    # copy content over
    unsafe_copyto!(dest_ptr, src_ptr, res_len)
end

```

I am getting an error, but I want to check my understanding. In the code above, I wanted to copy some memory over from one location to another. So to do that I don’t want the memory location to change, so I preserve `missing_bytes` and `res` so that their memory location will not change for duration of the `begin` and `end` block. Is my understand correct? So if the code fails, it must be because of either

1. my operation is illegal
2. There is Julia bug (highly unlikely)

and I can rule the data having moved location while that code is running causing issues. Right?

---

<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: [May 27, 2020, 12:10pm UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/2 "2020-05-27T12:10:11Z")

</div>

`GC.@preserve`, or the GC in general does nothing about the “location” of variables/arrays, it makes sure the object is valid and all derived pointer from it are valid unless explicitly invalidated otherwise, for example, if you push an element to an array. For well defined object the use of `GC.@preserve` looks correct assuming no race or other undefined behavior (e.g. out of bound access).

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [May 27, 2020, 12:25pm UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/3 "2020-05-27T12:25:44Z")

</div>

> [@xiaodai](#):
>
> `pointer(res, res_len+1)`

If `res_len` is the length of `res`, do you mean to be point to its end?

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [May 27, 2020, 12:52pm UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/4 "2020-05-27T12:52:02Z")

</div>

> [@Elrod](#):
>
> If `res_len` is the length of `res` , do you mean to be point to its end?

I am looking at `Vector{Union{Missing, T}}`. The missing indicators are located past the `T` values, hence looking past that in this is the right way to go.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [May 27, 2020, 12:53pm UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/5 "2020-05-27T12:53:18Z")

</div>

so basically it just ensures my `pointer` calls will return the location I wanted. Anyway, have not encountered any memory crashes after I fixed the bugs, so my understanding of `GC.@preserve`, although wrong didn’t lead me astray.

---

<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: [May 27, 2020, 1:08pm UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/6 "2020-05-27T13:08:26Z")

</div>

> [@xiaodai](#):
>
> The missing indicators are located past the `T` values, hence looking past that in this is the right way to go.

I’m pretty sure it may not be there. There could be gap in between. (Also I thought there’s a function to give you it’s location so you don’t need to do it yourself)

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [May 27, 2020, 1:35pm UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/7 "2020-05-27T13:35:04Z")

</div>

> [@yuyichao](#):
>
> Also I thought there’s a function to give you it’s location so you don’t need to do it yourself

Oh! I need to find it. If you can recall what it is please do let me know! It’s worked for me so far.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [May 27, 2020, 1:38pm UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/8 "2020-05-27T13:38:08Z")

</div>

I found some info here but it doesn’t exactly tell me a a function to find it [isbits Union Optimizations · The Julia Language](https://docs.julialang.org/en/v1/devdocs/isbitsunionarrays/)

---

<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: [May 27, 2020, 3:00pm UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/9 "2020-05-27T15:00:23Z")

</div>

See implementation of [julia/array.jl at 853fe04e303621a7d11db980905ab26979b8b1fc · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/853fe04e303621a7d11db980905ab26979b8b1fc/base/array.jl#L286)

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [May 27, 2020, 11:49pm UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/10 "2020-05-27T23:49:49Z")

</div>

I see it. It’s hard to interpret how I can use it in practice.

My use case is that I know where the missing are but the data I want to copy are not in the form of a complete `Vector{Union{Missing, T}}`. But code just shows how to use them with `Vector{Union{T, Missing}}` which is via a `ccall` to `jl_array_typetagdata`. So I can’t go deeper into `jl_array_typetagdata`.

---

<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: [May 28, 2020, 12:34am UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/11 "2020-05-28T00:34:10Z")

</div>

What else do you want to interpret? You are asking about a function to get the location of the tags and `jl_array_typetagdata` is the answer.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [May 28, 2020, 12:42am UTC](https://discourse.julialang.org/t/does-gc-preserve-a-b-preserves-the-location-of-a-and-b/40233/12 "2020-05-28T00:42:22Z")

</div>

> [@yuyichao](#):
>
> `jl_array_typetagdata` is the answer.

I see. I misinterpreted it! I thought it was the code to move the vectors but it’s actually the code that returns the location.
