原始分配了一个内存地址? [英] Is primitive assigned a memory address?

查看:114
本文介绍了原始分配了一个内存地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解后台声明和分配原始类型的过程。

I am trying to understand the process of declaration and assignment of a primitive type at the back stage.


  1. int i;

  2. i = 3;

  1. int i;
  2. i = 3;

对于1),在内存堆栈上,它分配一个空间用于存储名为i
的int类型值。对于2),它将值3分配给保留的空间上面

For 1), on the memory stack, it assigns a space for storing an int type value named i For 2), it assigns the value 3 to the space preserved above

那里有内存地址吗?
从我的印象来看,内存地址总是与堆上的对象相关联?

Is there a memory address there? From my impression, memory address is always associated with the objects on the heap?

更新:

关于回复:

因此,对于堆栈中的每个变量,它们都被分配了一个内存地址,就像对象一样在堆上。我是否正确?

So, for every variable on the stack, they are all assigned a memory address as well just like the objects on the heap. Am I correct?

但对于Java,情况并非如此?

But for Java, this is not the case?

推荐答案

并非总是涉及地址。如果编译器发现它们的地址从未被程序员占用,则编译器可以将变量放入寄存器中。所以你不需要任何访问主内存的权限。例如,在上面的代码中,编译器可以生成的内容可以简单到

There are not always addresses involved. The compiler can put variables into registers if it finds that their address is never taken by the programmer. So you wouldn't need any access to the main memory. For example in your code above, what the compiler could generate could be as simple as

add $2, $0, 3

将值3放入寄存器2.只要创建指针并使其指向该变量,实际上你有一个地址。然后变量不能再在寄存器中了。

to put value 3 into register 2. As soon as you create a pointer and make it point to that variable, then you have an address, actually. And then the variable cannot be in a register only anymore.

这篇关于原始分配了一个内存地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆