淺析C語言的一個關鍵字——register

 

轉載自: http://zqwt.012.blog.163.com/blog/static/1204468420107745836163/

淺析C語言的一個關鍵字——register

1、register修飾符暗示編譯程序相應的變量將被頻繁地使用,如果可能的話,應將其保存在CPU的寄存器中,以加快其存儲速度。例如下面的內存塊拷貝代碼

/* Procedure for the assignment of structures, */

/* if the C compiler doesn't support this feature */

  #ifdef NOSTRUCTASSIGN

  memcpy (d, s, l)

{

register char *d;

  register char *s;

  register int i;

  while (i--)

  *d++ = *s++;

  }

#endif

 

2、但是使用register修飾符有幾點限制

(1)register變量必須是能被CPU所接受的類型。

這通常意味着register變量必須是一個單個的值,並且長度應該小於或者等於整型的長度。不過,有些機器的寄存器也能存放浮點數。

(2)因爲register變量可能不存放在內存中,所以不能用“&”來獲取register變量的地址。

(3)只有局部自動變量和形式參數可以作爲寄存器變量,其它(如全局變量)不行。

在調用一個函數時佔用一些寄存器以存放寄存器變量的值,函數調用結束後釋放寄存器。此後,在調用另外一個函數時又可以利用這些寄存器來存放該函數的寄存器變量。

(4)局部靜態變量不能定義爲寄存器變量。不能寫成:register static int a, b, c;

(5)由於寄存器的數量有限(不同的cpu寄存器數目不一),不能定義任意多個寄存器變量,而且某些寄存器只能接受特定類型的數據(指針和浮點數),因此真正起作用的register修飾符的數目和類型都依賴於運行程序的機器,而任何多餘的register修飾符都將被編譯程序所忽略。

 

注意:

  早期的C編譯程序不會把變量保存在寄存器中,除非你命令它這樣做,這時register修飾符是C語言的一種很有價值的補充。然而,隨着編譯程序設計技術的進步,在決定哪些變量應該被存到寄存器中時,現在的C編譯環境能比程序員做出更好的決定。實際上,許多編譯程序都會忽略register修飾符,因爲儘管它完全合法,但它僅僅是暗示而不是命令。

 

 

  

register

The storage class specifier register stores a variable’s data in a hardware CPU register (if available) instead of memory.

For example,

the definition

register int num;

uses a register for the integer num.

Only nonstatic, local variables may reside in registers, and C++ uses the same rules for register variable scope and initialization as it does with automatic variables.

You cannot take the address of a register with &, use static with registers, or declare register variables outside of functions.

 

NOTE

Register variables are highly machine and compiler dependent.

Many compilers, for instance, allocate registers for only pointers, int, and char data types. Furthermore, your compiler may choose to ignore all of your register declarations or give you a register even if you don’t ask for one! Consult your compiler’s documentation to see how to use register variables effectively.

 

Why use register variables?

In time-critical code, register variables can improve a program’s performance.

Arithmetic and array subscripting operations inside loops usually execute faster with register variables than with auto or static variables.

Loop variables, pointers, and function parameters are also suitable candidates for register variables.

Registers are a limited resource, so you’ll want to allocate them carefully.

When the compiler runs out of hardware CPU registers, variables that you declare register become automatic.

 

The following code, for example, uses a register variable to loop through a large array if it’s time to process data.

 

 if (process) {

  for (register int i = 0; i < huge; i++)

{

    . . . a[i] . . . 

 }

Inside the for loop, the program declares i as a register variable before it loops through the array. If a register is available, the loop executes faster than it would without one.

 

NOTE

What should you do when there are not enough registers?

If this situation arises, declare your registers outside of loops and declare the most important register variables first.

This approach makes the least important variables become local variables if the compiler cannot provide registers.

 

A good application of this technique is with multidimensional array subscripts. The following code, for example, loops through all the elements of a two-dimensional array of integers declared as b[imax][jmax].

 

 for (register int i = 0; i < imax; i++) 

   for (register int j = 0; j < jmax; j++)

. . . b[i][j] . . .

The compiler, however, is likely to allocate a register first for i, not j (if only one register is available). The following approach is arguably better and more portable for time-critical code.

 

register int j;

register int i; 

 for (i = 0; i < imax; i++) 

  for (j = 0; j < jmax; j++)

. . . b[i][j] . . .

Since the second for loop executes more often than the first for loop, it’s more important for the compiler to provide a register for j than for i.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章