浅析C语言的一个关键字——register

来源:互联网 发布:mac关闭safari 编辑:程序博客网 时间:2024/04/29 01:46

 

转载自: 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.

原创粉丝点击