朱莉娅不变的物体的地址

发布于 2025-02-05 11:33:40 字数 291 浏览 2 评论 0原文

这个问题

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

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

获取不变对象的内存地址?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唠甜嗑 2025-02-12 11:33:40

不变的对象不一定存在在内存中(它们可能存储在寄存器中,或者根本不存在),因此您无法获得其地址。澄清示例:

  • “数组a = [0]生活在哪里?” 这很有意义,因为a = [ 0]是一个特定的数组,一次分配了一次,并且必须生活在内存中的特定位置,因为该数组/内存的所有观察者都必须在某人修改时查看:如果某人进行a [1] = 1然后有一个内存位置,该内存位置从持有值0到值1


  • “整数在哪里生活在哪里? > 0 到处都是,它们都是“相同的零”,因为它们是无法区分和可互换的。无法将零值更改为另一个值,只能更改某些可突变容器(带位置)的值。


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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文