English 中文(简体)
执行期间可变变化的地址?
原标题:Can the address of variable change during execution?

我读的是包含变量的地址,并想知道执行期间的变数变化的地址是否在任何情况下?它会不会因为JVM或CLR的垃圾收集而发生?C和C++呢?

最佳回答

这在很大程度上取决于语言, 以及您所说的 更改地址 < / em> 的含义。 在 C++ 中, 对象的地址是独特的识别点, 无法移动 。 有些管理的语言( 包括 Java 和 C# ) 使用代代代垃圾收集器, 这些垃圾收集器在记忆中实际移动对象( 在代代际GC中, 对象会随着时间从一代移到另一代 ), 但事实是您不应该在意 : 在您可以获得对象地址的那些语言中, 地址是固定的, 在固定地址的那些语言中, 它不是对象的 < em > 可接收 < / em > 状态的一部分 。

请注意, 我使用的是 < em> object < / em >, 而不是这里的变量, 因为我相信这就是你感兴趣的。 有一条细线可以区分对象和变量, 例如, 在 Java, 引用是一个变量, 可用于访问对象。 但我甚至不会尝试去查看其中任何一个的精确定义...

问题回答

If your code has Undefined Behaviors then all bets are off and yes it might happen.
But in a valid C/C++ program the address of an variable will not change.

在有效的 C/C++ 程序中,除非您的代码故意或以其他方式更改变量的地址,否则执行环境不会改变地址。

它不可能改变,但不能以它不会改变的假设作为代码的基础。

在爪哇,某些变量的地址(例如静态或实例字段)可能会改变,而其他变量通常会改变(例如本地变量)t。对于当代的JVMs来说,线串堆在堆积层外的记忆部分,而GC不会移动它们。

然而,对于“使用变量的地址”这一点没有实际意义,因为你不能在爪哇这样做。 (如果可以,那么理事会有责任处理搬迁问题。 )

基本上, 变量继续指程序执行时间中的相同值或内存位置 。 但是, 这一点可以更改, 使 < code> 无法初始化 (具有未定义的值), 所以它尚未被明确给定一个特定值 。 这可能会带来无法预测的结果 。 要处理这样的问题, 只有当变量首次使用时, 内存变量所需的空间才能分配, 并且当该变量不再需要时, 才能解开 。

在 c++ 中, 地址不会改变 。

请注意, 在 c+++ 中, 您可以推翻 & amp; 运算符, 尽管对于类变量来说, 所以即使地址不会改变, 您可能从 & amp; 可变得到的值也可能改变( 这不是地址变化, 但是可能会混淆) 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...