如何存储在内存变量名用C? [英] How are variable names stored in memory in C?

查看:119
本文介绍了如何存储在内存变量名用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C,假设你有一个名为变量 VARIABLE_NAME 。比方说,它位于 0xaaaaaaaa ,并在内存地址,换句话说有整数123.所以, VARIABLE_NAME 包含123。

In C, let's say you have a variable called variable_name. Let's say it's located at 0xaaaaaaaa, and at that memory address, you have the integer 123. So in other words, variable_name contains 123.

我在寻找周围澄清措辞 VARIABLE_NAME 位于 0xaaaaaaaa 。如何编译器识别出字符串变量名被与该特定存储器地址相关?是字符串变量名的地方存储在内存中?该编译器刚刚替补 VARIABLE_NAME 0xaaaaaaaa 每当看到它,如果是这样,是不是必须使用内存为了使该替换吗?

I'm looking for clarification around the phrasing "variable_name is located at 0xaaaaaaaa". How does the compiler recognize that the string "variable_name" is associated with that particular memory address? Is the string "variable_name" stored somewhere in memory? Does the compiler just substitute variable_name for 0xaaaaaaaa whenever it sees it, and if so, wouldn't it have to use memory in order to make that substitution?

推荐答案

变量名称不存在了之后,编译运行(除非特殊情况下,像在共享库或调试符号导出全局)。编译整个行动的目的是把这些符号名和算法重新由源$ C ​​$ C psented $ P $,把它们变成本地机器指令。所以,是的,如果你有一个全球性的 VARIABLE_NAME ,以及编译器和链接决定把它放在 0xaaaaaaaa ,那么无论它在code时,它只是可以通过该地址进行访问。

Variable names don't exist anymore after the compiler runs (barring special cases like exported globals in shared libraries or debug symbols). The entire act of compilation is intended to take those symbolic names and algorithms represented by your source code and turn them into native machine instructions. So yes, if you have a global variable_name, and compiler and linker decide to put it at 0xaaaaaaaa, then wherever it is used in the code, it will just be accessed via that address.

因此​​,要回答你的问题的文字:

So to answer your literal questions:

如何编译器识别出字符串变量名被与该特定存储器地址相关?

How does the compiler recognize that the string "variable_name" is associated with that particular memory address?

工具链(编译器和放大器;连接器)共同分配的内存位置的变量。这是编译器的工作是让所有引用的轨道,后来把连接在正确的地址。

The toolchain (compiler & linker) work together to assign a memory location for the variable. It's the compiler's job to keep track of all the references, and linker puts in the right addresses later.

在某处存储在内存中字符串变量名

Is the string "variable_name" stored somewhere in memory?

仅在编译的运行。

该编译器刚刚替补 VARIABLE_NAME 0xaaaaaaaa 每当看到它,如果是这样,不会吧需要使用内存以便使该替换吗?

Does the compiler just substitute variable_name for 0xaaaaaaaa whenever it sees it, and if so, wouldn't it have to use memory in order to make that substitution?

是的,那是pretty多发生了什么,除了这两个阶段的工作与链接器。是的,它使用的内存,但它的的编译器的内存,在运行时你的程序没有什么。

Yes, that's pretty much what happens, except it's a two-stage job with the linker. And yes, it uses memory, but it's the compiler's memory, not anything at runtime for your program.

一个例子可以帮助你理解。让我们来尝试一下这个方案:

An example might help you understand. Let's try out this program:

int x = 12;

int main(void)
{
    return x;
}

pretty简单的,对不对?好。让我们这个程序,并编译它,并期待在拆卸:

Pretty straightforward, right? OK. Let's take this program, and compile it and look at the disassembly:

$ cc -Wall -Werror -Wextra -O3    example.c   -o example
$ otool -tV example
example:
(__TEXT,__text) section
_main:
0000000100000f60    pushq   %rbp
0000000100000f61    movq    %rsp,%rbp
0000000100000f64    movl    0x00000096(%rip),%eax
0000000100000f6a    popq    %rbp
0000000100000f6b    ret

请参阅 MOVL 行?它抓住全局变量(在指令指针相对的方式,在这种情况下)。否 X

See that movl line? It's grabbing the global variable (in an instruction-pointer relative way, in this case). No more mention of x.

现在让我们更加有点复杂,并添加一个局部变量:

Now let's make it a bit more complicated and add a local variable:

int x = 12;

int main(void)
{  
    volatile int y = 4;
    return x + y;
}

本方案的拆卸是:

The disassembly for this program is:

(__TEXT,__text) section
_main:
0000000100000f60    pushq   %rbp
0000000100000f61    movq    %rsp,%rbp
0000000100000f64    movl    $0x00000004,0xfc(%rbp)
0000000100000f6b    movl    0x0000008f(%rip),%eax
0000000100000f71    addl    0xfc(%rbp),%eax
0000000100000f74    popq    %rbp
0000000100000f75    ret

现在有两个 MOVL 指令和 ADDL 指令。你可以看到第一个 MOVL 正在初始化,它的决定将是在栈(基指针 - 4 )。那么接下来 MOVL 的是全局 X 到寄存器 EAX ADDL 补充到该值。但正如你所看到的,字面 X 字符串不存在了。他们对便利的的,程序员,但电脑当然不会在执行时关心他们。

Now there are two movl instructions and an addl instruction. You can see that the first movl is initializing y, which it's decided will be on the stack (base pointer - 4). Then the next movl gets the global x into a register eax, and the addl adds y to that value. But as you can see, the literal x and y strings don't exist anymore. They were conveniences for you, the programmer, but the computer certainly doesn't care about them at execution time.

这篇关于如何存储在内存变量名用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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