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)
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)