显式定义变量地址(在Go语言中)

huangapple go评论88阅读模式
英文:

explicitly defining variable addresses (in Go)

问题

对于一个程序员来说,跟踪变量的地址以便将变量的地址用作数据的指针,这样做是否实用?

我正在努力理解在Go语言中如何使用指针存储和引用变量的地址。

作为一个一般原则,直接赋值变量的地址是否有用?我可以想象一种情况,即数据可以被编码在变量的物理(虚拟)地址中,而不仅仅是该变量的值。

例如,第1000个顾客已经购买了500美元的商品。我可以在位置1000存储一个值为500的整数吗?

我知道通常可以使用数组来实现这样的功能,其中位置999的变量对应于第1000个顾客,但我的问题不是关于数组,而是关于直接赋值地址。

假设我正在处理数十亿个对象。有没有一种简单的方法可以将地址作为对象数据的一部分,并且存储在该位置的值作为不同的数据?

例如,地址为135851851904的整数存储了一个值为46876,地址为135851851905的整数存储了一个值为123498761,等等。我想在这种情况下,使用数组或切片将会非常庞大而不高效。

顺便说一句,如果我的问题是由于误解而引起的,是否有人可以提供一个详细但易于理解的资源来解释这个主题?我一直找不到一个真正解释细节的好资源。

英文:

Simplified Question:

Is it practical for a programmer to keep track of the addresses of variables, so that a variable's address can be used as a point of data on that variable?

Original Question:

I am attempting to wrap my head around how variables are stored and referenced by address using pointers in Go.

As a general principal, is it ever useful to assign a variable's address directly? I can imagine a situation in which data could be encoded in the physical (virtual) address of a variable, and not necessarily the value of that variable.

For instance, the 1000th customer has made a 500 dollars of purchases. Could I store an interger at location 1000 with a value of 500?

I know that the common way to do something like this is with an array, where the variable at position 999 corresponds to the 1000th customer, but my question is not about arrays, it's about assigning addresses directly.

Suppose I'm dealing with billions of objects. Is there an easy way to use the address as part of the data on the object, and the value stored at that location as different data?

for instance, an int at address 135851851904 holds a value of 46876, 135851851905 holds 123498761, etc. I imagine at this point an array or slice would be far too large to be efficient.

Incidentally, if my question due to a misunderstanding, is there a resource someone can provide which explains the topic in deep, but understandable detail? I have been unable to find a good resource on the subject that really explains the details.

答案1

得分: 6

有时直接分配变量的地址是有用的吗?

你可以使用unsafe包来实现这一点,但是这样做的想法是,除非你有一个具体的、无法解决的使用案例需要它,否则不要这样做。

我可以将一个值为500的整数存储在地址为1000的位置吗?

如前所述,这是可能的,但选择一个任意的地址并不会有太大帮助,因为它可能甚至没有被映射。如果你写入这样的位置,你将会得到一个访问冲突错误(并且你的程序将崩溃)。如果你碰巧命中一个有效的地址,你很可能会覆盖程序运行所需的其他数据。

有没有一种简单的方法可以将地址作为对象数据的一部分,而将存储在该位置的值作为不同的数据?

一般来说,没有。

如果你成功构建了一种代数结构,在你自己的指针算术操作下,它在一组有限的地址中是封闭的,并且你可以保证它始终是一个有效的虚拟内存段,那么是的,但这违背了使用垃圾回收语言的初衷。此外,阅读这样的程序将会非常困难。

英文:

> is it ever useful to assign a variable's address directly?

You can use the unsafe package to achieve that but the idea is that you don't do it unless you have a concrete and otherwise unsolvable use-case that requires it.

> Could I store an interger at location 1000 with a value of 500?

As mentioned before it is possible but choosing an arbitrary address won't get you far because it may not even be mapped. If you write to such a location you'll get a access violation (and your program will crash). If you happen to hit a valid address number you'll likely be overwriting other data that your program needs to run.

> Is there an easy way to use the address as part of the data on the object, and the value stored at that location as different data?

In general no.

If you managed to build some kind of algebraic structure closed under the operations by which your own pointer-arithmetic is defined in a finite set of addresses which you can guarantee to always be a valid virtual memory segment then yes but it defeats the purpose of using a garbage collected language. Additionally it would be hell to read such a program.

huangapple
  • 本文由 发表于 2014年11月4日 21:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/26735786.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定