Register Variables

Data in the machine-language version of a program is normally loaded into registers for calculations and other processing.

The compiler might ignore register declarations. For example, there might not be a sufficient number of registers available. The following definition suggests that the unsigned int variable counter be placed in one of the computer’s registers; regardless of whether the compiler does this, counter is initialized to 1:

register unsigned int counter = 1;

The register keyword can be used only with local variables and function parameters.


Image Performance Tip 6.2

The storage-class specifier register can be placed before an automatic variable declaration to suggest that the compiler maintain the variable in one of the computer’s high-speed hardware registers rather than in memory. If intensely used variables such as counters or totals are kept in hardware registers, the overhead of repeatedly loading the variables from memory into the registers and storing the results back into memory is eliminated.



Image Performance Tip 6.3

Often, register is unnecessary. Today’s optimizing compilers can recognize frequently used variables and may place them in registers without needing a register declaration.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.10.162