PDA

View Full Version : What is register int?



kiboi
6th November 2012, 10:27
What is a register int?
What is the difference between a register int and a normal int?
Why not use register int where we can instead of using a normal int?

Santosh Reddy
6th November 2012, 11:37
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 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 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)

kiboi
6th November 2012, 23:27
Thanks for that very informative answer!