register int var v.s. int var

register int var v.s. int var


Microsoft Specific
The compiler does not accept user requests for register variables; instead, it makes its own register choices when global register-allocation optimization (/Oe option) is on. However, all other semantics associated with the register keyword are honored.


END Microsoft Specific
ANSI C does not allow for taking the address of a register object; this restriction does not apply to C++. However, if the address-of operator (&) is used on an object, the compiler must put the object in a location for which an address can be represented. In practice, this means in memory instead of in a register.


https://msdn.microsoft.com/en-us/library/482s4fy9(v=vs.100).aspx




What is a register int?
register int storage area is CPU registers


What is the difference between a register int and a normal int?
The only diffrence is storage area. Normal ints are stored in RAM.


Why not use register int where we can instead of using a normal int?
Typical CPU will have 5-30 (Completely depends on architecture) general pupose registers, so they are very few. One have to use them wisely. Most of the compilers will handle this for you, they will try to fit normal int to registers, and if registers are not available normal int is used even for register int. (Note you may need to set the optimization level in compiler)


Variables which are used very often (many statments) are typically declared as register int
Examples:
1. Loop control varables
2. Array offset index
3. Pointers (specially array pointers, memory pointers)


Main reason behind this is that CPU can operate on data in registers only, so it has to load the variables in to registers and then operate on them. If we already have frequenty used data in registers, loading time is saved (which is significant on CPUs using external memory)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值