简体   繁体   English

什么时候为变量分配内存空间?

[英]When is memory space allocated to a variable?

Does the compiler allocate 4 bytes of memory when the variable is declared:声明变量时编译器是否分配了 4 个字节的内存:

int a;

Or does it allocate memory when a value is assigned to it:或者当一个值被分配给它时它是否分配内存:

a = 5;

When is memory allocated?什么时候分配内存? During variable declaration or initialization?在变量声明或初始化期间?

The variable is allocated when the structure containing it is allocated.在分配包含它的结构时分配变量。

For a local variable in a method this is (with some caveats) when the method is invoked.对于方法中的局部变量,这是在调用方法时(有一些注意事项)。

For a static variable this is when the class is "initialized" (which happens some time after it's loaded and before it's first used).对于静态变量,这是“初始化”类的时间(发生在它加载后和第一次使用之前的一段时间)。

For an instance variable this is when the instance is created.对于实例变量,这是创建实例的时间。

In most programming languages the compiler is allowed to choose when to allocate space for variables.在大多数编程语言中,编译器可以选择何时为变量分配空间。 The only thing you are guaranteed, is that the storage will be available if and when you need it.您唯一可以保证的是,存储将在您需要时可用。

A short anecdote...一个简短的轶事...

The C programming language used to require that all variables used in a method be declared at the top of the method.过去的 C 编程语言要求在方法的顶部声明方法中使用的所有变量。 This was because compilers used to reserve storage for all stack (local) variables in a method as soon as you entered the method.这是因为编译器曾经在您进入方法后立即为方法中的所有堆栈(局部)变量保留存储空间。 Today that requirement doesn't exist in part because compilers are a lot smarter.今天这个要求不存在的部分原因是编译器要聪明得多。

Most compilers of C-like languages will defer allocation of instances until first use for optimized code.大多数类 C 语言的编译器会推迟实例的分配,直到第一次用于优化代码。 The REALLY tricky thing here is that the first use may not be where you think it is, and it may not occur at all.这里真正棘手的是,第一次使用可能不在您认为的位置,而且可能根本不会发生。 For example, if you have the following code:例如,如果您有以下代码:

int foo(int x) {
  int y = 5;
  if (x > 5)
    y += x;
  return y;
}

You might think the first use is when you assign 5 to y, but the compiler could optimize that code to something more like:您可能认为第一次使用是当您将 5 分配给 y 时,但编译器可以将该代码优化为更类似的内容:

int foo(int x) {
  if (x > 5)
    return 5 + x;
  return 5;
}

In this code y never really exists at all.在这段代码中,y 根本不存在。

TL;DR - The compiler actually isn't guaranteed to allocate memory on declaration or use. TL;DR - 编译器实际上并不能保证在声明或使用时分配内存。 Trust the compiler, it is (usually) smarter than us all.相信编译器,它(通常)比我们所有人都聪明。

When we have “ declared '' a variable, we have meant that we have told the compiler about the variable;当我们“声明”一个变量时,意味着我们已经告诉编译器有关该变量的信息; ie its type and its name, as well as allocated a memory cell for the variable (either locally or globally).即它的类型和名称,以及为变量(本地或全局)分配的内存单元。 This latter action of the compiler, allocation of storage, is more properly called the definition of the variable.编译器的后一个动作,存储分配,更恰当地称为变量的定义

Simply Definition = Variable Declaration + Variable initialization简单定义= 变量声明 + 变量初始化

JVM divides memory into stack and heap memory. JVM 将内存分为栈内存和堆内存。 Whenever we declare new variables and objects, call new method, declare a String or perform similar operations, JVM designates memory to these operations from either Stack Memory or Heap Space.每当我们声明新变量和对象、调用新方法、声明字符串或执行类似操作时,JVM 都会从堆栈内存或堆空间为这些操作指定内存。

  • Local variables are created in the stack局部变量在栈中创建
  • Instance variables are created in the heap & are part of the object they belong to.实例变量在堆中创建并且是它们所属对象的一部分。
  • Reference variables are created in the stack.引用变量在堆栈中创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM