存储非指针值时,C中实际发生了什么? [英] What is actually going on in C when a non-pointer value is stored?

查看:91
本文介绍了存储非指针值时,C中实际发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重要提示::该操作试图一次询问太多的问题,并且具有误导性,因为我在关于如何使用指针的错误假设下编写了它,最终看起来就像一个重复项.请改为查看此内容:变量如何与C中的值绑定?

IMPORTANT: This tried to ask too many things at once and was misleading because I wrote it under a false assumption about how pointers can be used, and it ended up just looking like a duplicate. Please see this instead: How are variables tied to their values in C?

让我们考虑在地址0001处有一个值4,然后将地址0001分配给变量num.我们可以将其可视化为两个表:

Let's consider that there is a value 4 at address 0001, and then we assign the address 0001 to the variable num. We could visualize this as two tables:

VARIABLE|ADDRESS    ADDRESS|VALUE
num     |0001       0001   |4

据我了解,这将是以下代码的最终产品:

From what I understand, this would be the end product of the following code:

int temp = 4;
int * num = &temp;

但是,第一行int temp = 4;是怎么回事?第一行会产生这样的内容吗?

However, what is going on at the first line, int temp = 4;? Does that first line produce something like this?

VARIABLE|ADDRESS    ADDRESS|VALUE
        |           temp   |4

指针的指针如何工作?代码是否会:

And how do pointers to pointers work? Would the code:

int temp = 4;
int * num = &temp;
int ** pnum = #

产生这个吗?

VARIABLE|ADDRESS    ADDRESS|VALUE
num     |0001       0001   |4
pnum    |0002       0002   |0001

想到这个的正确方法是什么?实际情况到底是怎么回事?另外,当存储结构而不是数字时,这会如何变化?

What is the right way to think of this? What is actually going on under the hood? Also, how does this change when a struct is stored instead of a number?

我了解上述示例可能完全不正确;他们只是将我的问题具体化.

I understand that the above examples are probably entirely incorrect; they were simply to contextualize my question.

推荐答案

并不是所有的变量都需要在内存系统中有一个地址,有些变量的寿命很短,因此它们可以在寄存器中保留整个寿命.在这种情况下,它们会由编译器分配(重命名)为eaxebxr1r2之类的东西.寄存器是CPU中可以容纳变量内容的插槽.

Not all variables need to have an address in the memory system, some variables are short lived enough that they can live their whole life-span in registers. In such a case, they get allocated (renamed) by the compiler to things like eax, ebx or r1, r2. Registers are slots in the CPU that can hold variable contents.

由于许多架构的寄存器数量有限(x86-64中有8个虚拟(对机器语言可见)寄存器,IA64中有256个寄存器...)其余变量被分配(编译)到内​​存中的地址,它将始终位于堆栈中.堆栈(由esp寄存器(一个特殊的寄存器跟踪))是一个后进先出的分配器,具有对操作系统的支持(随着内存页面的增长,内存页面会不断增长),因此编译器只需采用当前的堆栈指针并对其进行递增根据要分配的变量的大小,这就是变量的地址.

Because lots of architecture have limited register numbers (8 virtual (visible to the machine language) registers in x86-64, 256 registers in IA64...) the rest of the variables get allocated (compiled) to an address in memory, which will always be on the stack. The stack (tracked by esp register, a special register) is a last in first out allocator with support of the operating system (memory pages get live as it grows) so the compiler just has to take the current stack pointer and increment it by the size of the variable to allocate, and that's the variable's address.

值分配是通过发出带有硬编码值的mov汇编命令来完成的,因此常量存在于程序的存储空间中.这意味着该值来自指令本身. (L1:inst缓存-> fetcher-> CPU管道-> mov-> STORE管道-> L1:数据缓存)

Value assignation, like in the first case you demonstrated, is done by issuing a mov assembly command with a hardcoded value, the constant thus exist in the memory space of the program. Which means the value comes from the instruction itself. (L1:inst cache->fetcher->CPU pipeline->mov->STORE pipeline->L1:data cache)

其余的工作就像您感觉到的那样.

The rest works like you sensed.

这篇关于存储非指针值时,C中实际发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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