Address of immutable object in Julia

Viewed 66

In this question, it is shown how to get the memory address of a mutable address

julia> a = [1, 2, 3]
3-element Array{Int64,1}:
 1
 2
 3

julia> s=repr(UInt64(pointer_from_objref(a)))
"0x000000001214ce80"

How to get the memory address of an immutable object?

1 Answers

Immutable objects don't necessarily exist in memory (they may be stored in registers, or not exist at all) so you can't get their address. Clarifying examples:

  • It makes sense to ask "Where does the array a = [0] live in memory?" This makes sense because a = [0] is a specific array, allocated once and which must live in a specific place in memory because it must be possible for all observers of that array/memory to see when someone has modified it: if someone does a[1] = 1 then there's a memory location that is changed from holding the value 0 to the value 1.

  • It does not make sense to ask "Where does the integer 0 live in memory?" It doesn't live anywhere specific—there are many instances of 0 all over the place and they are all "the same zero" in the sense that they are indistinguishable and interchangeable. There's no way to change the value of zero to another value, it is only possible to change what value some mutable container (with location) refers to.

Related