原语是否分配了内存地址?

发布于 2024-07-09 12:47:25 字数 342 浏览 7 评论 0原文

我试图了解后台原始类型的声明和赋值的过程。

  1. int i;
  2. 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.

  1. int i;
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

百思不得你姐 2024-07-16 12:47:25

并不总是涉及地址。 如果编译器发现程序员从未获取过变量的地址,则可以将变量放入寄存器中。 所以你不需要对主内存进行任何访问。 例如,在上面的代码中,编译器生成的内容可能很简单,只需

add $2, $0, 3

将值 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

add $2, $0, 3

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.

明月夜 2024-07-16 12:47:25

假设您正在谈论 C 或 C++(我不知道),是的。 您可以像这样访问该地址:

int i = 3;

int *k = &i; // k now is a pointer to i

*k = 4; // assigns the value k points to (i) to 4, i is now 4

Assuming you're talking about C or C++ (I can't tell), yes. You can access the address like so:

int i = 3;

int *k = &i; // k now is a pointer to i

*k = 4; // assigns the value k points to (i) to 4, i is now 4
韶华倾负 2024-07-16 12:47:25

堆栈缓冲区溢出还会如何发生? :) 有人必须写入指向堆栈的指针。

How else do stack buffer overflows occur? :) someone's got to be writing to a pointer to the stack.

浪菊怪哟 2024-07-16 12:47:25

实际上,堆栈是进程虚拟内存的特殊区域,因此堆栈中的所有内容都有内存地址。 由 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文