c - 为什么编译器将变量存储在寄存器中?

标签 c compiler-construction cpu-architecture cpu-registers

<分区>

嗨,我一直在阅读各种文档中的此类内容

register

Tells the compiler to store the variable being declared in a CPU register.

In standard C dialects, keyword register uses the following syntax:

register data-definition;

The register type modifier tells the compiler to store the variable being declared in a CPU register (if possible), to optimize access. For example,

register int i;

Note that TIGCC will automatically store often used variables in CPU registers when the optimization is turned on, but the keyword register will force storing in registers even if the optimization is turned off. However, the request for storing data in registers may be denied, if the compiler concludes that there is not enough free registers for use at this place.

http://tigcc.ticalc.org/doc/keywords.html#register

我的观点不仅仅是注册。我的观点是为什么编译器会将变量存储在内存中。编译器的任务就是编译并生成目标文件。在运行时发生实际的内存分配。为什么编译器会做这个生意。我的意思是,如果不通过编译文件本身来运行目标文件,内存分配是否会在 C 的情况下发生?

最佳答案

编译器正在生成机器代码,机器代码用于运行您的程序。编译器决定它生成什么机器代码,因此决定在运行时将发生什么样的分配。当您键入 gcc foo.c 时,它不会执行它们,但稍后,当您运行可执行文件时,运行的是 GCC 生成的代码。

这意味着编译器希望生成尽可能快的代码并在编译时做出尽可能多的决定,这包括如何分配东西。

关于c - 为什么编译器将变量存储在寄存器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19709683/

相关文章:

java - 虚拟机: Bytecode Confusion and JIT

asynchronous - 无时钟计算机芯片发生了什么?

c - 为什么 select 不向带有缓冲数据的文件描述符发出信号?

将 int** 数组转换为不同大小/组成的 char** 数组

c - 如何在 C 中为字符串数组分配内存 - malloc 错误

android - Android Studio 中具有 CPU 架构的多个构建变体

cpu - 如果 CPU 一直在执行指令,我们如何衡量它的工作量?

c - 如何在 C 中交换一行中的两个变量?

c# - JIT 编译器与离线编译器

c++ - 为什么编译器会忽略模板类中的结构元素?