# Memory address of a set, array, string,

**URL:** <https://discourse.julialang.org/t/memory-address-of-a-set-array-string/90717>\
**Category:** General Usage\
**Tags:** memory-address\
**Created:** [November 23, 2022, 3:25pm UTC](https://discourse.julialang.org/t/memory-address-of-a-set-array-string/90717 "2022-11-23T15:25:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vahid\_Hosseinzadeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vahid_hosseinzadeh/32/33293_2.png) [@Vahid\_Hosseinzadeh](https://discourse.julialang.org/u/Vahid_Hosseinzadeh)\
**Post date:** [November 23, 2022, 3:25pm UTC](https://discourse.julialang.org/t/memory-address-of-a-set-array-string/90717/1 "2022-11-23T15:25:28Z")

</div>

I run the following and get error

```julia
pointer_from_objref(Set([1,2,3])) 
#error pointer_from_objref cannot be used on immutable objects

```

however as I understand, Sets are mutable.  
About the strings and arrays, `pointer()` actually works. But what is `objectid()` then which works for anything? Is it the memory address?  
I searched but failed to get a coherent picture of what `pointer_from_objref`, `pointer`, `objectid`, `hash` … are doing.  
Appreciate your answers.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [November 23, 2022, 3:53pm UTC](https://discourse.julialang.org/t/memory-address-of-a-set-array-string/90717/2 "2022-11-23T15:53:42Z")

</div>

> [@Vahid\_Hosseinzadeh](#):
>
> however as I understand, Sets are mutable.

These functions are typically only used when calling foreign functions, e.g. C, Fortran, etc. Technically a julia `Set{Int}` is an immutable struct containing a `Dict{Int, Nothing}`, and immutable structs don’t have a memory address. They can live on the stack, in registers or in memory, as the compiler sees fit. You should use `Ref(...)` to obtain a reference to a julia object.

```julia
julia> s = Set([1,2,3])
Set{Int64} with 3 elements:
  2
  3
  1

julia> s.dict
Dict{Int64, Nothing} with 3 entries:
  2 => nothing
  3 => nothing
  1 => nothing

julia> s.dict = Dict([2=>nothing])
ERROR: setfield!: immutable struct of type Set cannot be changed
Stacktrace:
...

julia> s.dict[4] = nothing
julia> s
Set{Int64} with 4 elements:
  4
  2
  3
  1

```

---

<div class="post-metadata">

**Author:** ![Vahid\_Hosseinzadeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vahid_hosseinzadeh/32/33293_2.png) [@Vahid\_Hosseinzadeh](https://discourse.julialang.org/u/Vahid_Hosseinzadeh)\
**Post date:** [November 24, 2022, 7:00pm UTC](https://discourse.julialang.org/t/memory-address-of-a-set-array-string/90717/3 "2022-11-24T19:00:56Z")

</div>

Thank you for the answer. Now I understand the situation about sets.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [November 24, 2022, 9:16pm UTC](https://discourse.julialang.org/t/memory-address-of-a-set-array-string/90717/4 "2022-11-24T21:16:33Z")

</div>

There will surelly be good practical reasons for it… but it remains it is a very odd implementation of a set…

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [November 28, 2022, 1:00pm UTC](https://discourse.julialang.org/t/memory-address-of-a-set-array-string/90717/5 "2022-11-28T13:00:15Z")

</div>

This is also how sets are implemented in Java, and I think a bunch of other languages.  
You need almost all the same code for a HashSet as you do for a HashMap (a dictionary).  
So once you have implemented the dictionary you can just reuse it.

Cool thing about Julia in this case, is julia doesn’t actually store Singltons like `nothing`, so there is basically no scaling memory overhead for doing this.

```julia
julia> Base.summarysize([nothing, nothing, nothing])
40

julia> Base.summarysize([nothing, nothing, nothing, nothing])
40

julia> Base.summarysize([nothing, nothing, nothing, nothing, nothing])
40

```
