使用寄存器存储类声明变量时,使用了多少个寄存器? [英] How many registers are used when a variable is declared using register storage class?

查看:138
本文介绍了使用寄存器存储类声明变量时,使用了多少个寄存器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,对于使用register存储类声明为以下变量的变量,C编译器将使用多少个寄存器: register int a.我已阅读此答案使用c语言可以为存储类REGISTER提供多少个寄存器和什么样的寄存器,但了解得不多.我知道它可能与实现有关,但是此寄存器受到限制,那么编译器何时会忽略该声明或会生成错误?

My question is simply, how many registers will a C compiler use for a variable declared using register storage class as : register int a. I have read this answer How many register and what kind of register are available for the storage class REGISTER in c language but could not understand much. I know it can be implementation dependent, but then this registers are limited, so when would the compiler ignore the declaration or will it generate an error?

推荐答案

C11仍支持register:

声明具有存储类的对象的标识符 说明符寄存器建议对对象的访问速度与对对象的访问速度一样快 可能的.这些建议有效的程度是 实现定义的.

A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined.

注释:

该实现可以将任何寄存器声明简单地视为 自动声明.

The implementation may treat any register declaration simply as an auto declaration.

那么将使用多少个寄存器?一种可能的答案是无",因为该关键字可能会被忽略.

So how many registers will be used? One possible answer is 'none' because the keyword may be ignored.

也就是说,实际上,在任何现代平台上的实现都很难定义宽度大于典型平台"word"宽度的int.因此,如果实现尊重register int a;,则register int a不太可能分配给多个寄存器,而register int a可能是0,1或2个寄存器.

That said, in practice it's very rare for an implementation on any modern platform to define intwith a width that is wider than width of a typical platform 'word'. So if an implementation respects register int a; it's unlikely register int a will be allocated to more than one register and register int a may be 0,1 or 2 registers.

当然,在某些硬件上,并非所有寄存器的大小都相同,有时不同的指令会将寄存器视为单独的或合并的.多少个寄存器可能没有意义.

Of course on some hardware not all registers are the same size and sometimes different instructions treat registers as separate or joined. How many registers may not be meaningful.

正如其他人指出的那样,在C语言中不鼓励使用该关键字.事实上,它的含义现已从C ++中删除(尽管该关键字保留为保留).

As others point out use of the keyword is widely discouraged in C. Indeed its meaning now removed from C++ (though the keyword retained as reserved).

我认为这在C语言中将是严酷的,因为C语言仍然希望程序员最接近微控制器的环境来规定这些细节.

I think that would be draconian in C which still looks to micro-controller environments where programmers come closest to want to dictate such details.

所有所说的回到过去"(25年前)是我与一个编译器一起工作的,该编译器忽略了register,但将前两个(我认为)声明的变量用作循环计数器到寄存器,并在有问题的情况下使用通过首先声明大循环索引,该函数中的第三个循环最长,并且性能显着提高.

All that said 'back in the day' (25 years ago) I worked with a compiler that ignored register but allocated the first 2 (I think) declared variables used as loop counters to registers and in the case in question the third loop in the function was longest and performance noticeably improved by declaring the big loop index first.

这似乎是很久以前的事了,没有人可以将其视为现代体验.

That all seems a very long time ago and no one should take it as the modern experience.

这篇关于使用寄存器存储类声明变量时,使用了多少个寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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