原语是否分配了内存地址?
我试图了解后台原始类型的声明和赋值的过程。
int i;
i = 3;
对于1),在内存栈上,分配了一块空间,用于存储一个名为i的int类型值 对于2),它把值3赋给上面保留的空间,
那里有内存地址吗? 在我的印象中,内存地址总是与堆上的对象相关联?
更新:
关于回复:
因此,对于堆栈上的每个变量,它们都被分配了一个内存地址,就像堆上的对象一样。 我对么?
但对于Java来说,又何尝不是如此呢?
I am trying to understand the process of declaration and assignment of a primitive type at the back stage.
int i;
i = 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?
Update:
Regarding the replies:
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?
But for Java, this is not the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
并不总是涉及地址。 如果编译器发现程序员从未获取过变量的地址,则可以将变量放入寄存器中。 所以你不需要对主内存进行任何访问。 例如,在上面的代码中,编译器生成的内容可能很简单,只需
将值 3 放入寄存器 2 中即可。一旦您创建了一个指针并使其指向该变量,实际上您就拥有了一个地址。 那么变量就不能再只存在于寄存器中了。
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
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.
假设您正在谈论 C 或 C++(我不知道),是的。 您可以像这样访问该地址:
Assuming you're talking about C or C++ (I can't tell), yes. You can access the address like so:
堆栈缓冲区溢出还会如何发生? :) 有人必须写入指向堆栈的指针。
How else do stack buffer overflows occur? :) someone's got to be writing to a pointer to the stack.
实际上,堆栈是进程虚拟内存的特殊区域,因此堆栈中的所有内容都有内存地址。 由 ESP (SP) 注册表(x86 架构)持有的堆栈头。 堆栈地址通常低于内存地址,因为堆栈比堆更接近进程虚拟内存的开头。
Really stack is special area of proccess virtual memory, so everything in stack has memory addres. Head of the stack holded by ESP (SP) registry (x86 architecture). Stack addresses usualy lower than memory adresses, because stack located closer to begining of proccess vitrual memory then heap.