Should I use the register keyword in my code?

Should I use the register keyword in my phone variable? I have it:

 void *anything(Caller *caller) { register void *phone = caller->phone; /* or this */ void *phone = caller->phone; if (phone) { return phone; } return NULL; } 

Is there any difference? What should I do?

+6
c
source share
6 answers

The register keyword is intended as a hint for optimization for the compiler. The problem is that the compiler knows your code better than you do, and these days you don't need such simplified hints to generate better code.

So, the only thing register does with modern compilers does not allow you to use & to accept the address of a variable. What is it.

+10
source share

There is no “special rule” for register used in conjunction with void * , register will work on void * as with any variable, suggesting to the compiler that such a variable will be heavily used and that it should be placed in the CPU register.

However, in principle, there is no reason to use register anywhere, since the optimizers included in current compilers are much better than programmers, guessing which variables should fit in registers, and will almost always happily ignore the register keyword completely.

The C99 standard also contains some additional information about register (§6.7.1 ¶4):

Declaring an identifier for an object with a register storage class specifier implies that access to the object should be as fast as possible. The extent to which such proposals are effective is determined upon implementation. 101)

101) An implementation can consider any register declaration simply as an auto declaration. However, regardless of whether address storage is actually used, the address of any part of an object declared with the storage class specifier register cannot be calculated explicitly (using the unary & operator, as described in 6.5.3.2), or implicitly (by name conversion array to pointer, as described in 6.3.2.1). Thus, the only statement that can be applied to an array declared using the register storage class specifier is sizeof .

Thus, using register , you are likely to get only the shortcomings of pretending to be variables in the register. :) Please note that the C ++ standard eases these restrictions, but obviously, if you try to take the address of the register variable, it will no longer remain in register.

In short, register is basically like a relic of the past, avoiding this is the best thing you can do with it. :)

+5
source share

The register keyword tells the compiler that you want this variable to be stored in the CPU register, and not in the system RAM. In general, this is the idea of ​​BAD, most compiler optimizers can significantly improve the work with variables, and many compilers generally ignore the keyword.

Just delete it.

+3
source share

As far as I remember, the register keyword tells the compiler to try to save the variable in the CPU register in order to increase the principle of locality. The programmer knows that the variable will be available many times, so he wants to save it in the fastest memory cell that he can get.

I suspect that many compilers are smarter today than (average) programmers when it comes to this kind of optimization, so this may not be necessary. I would not use it.

+2
source share

It looks like you shouldn't use registers at all.

Registers are instructions for compilers in which to place variables. They have no types. The fact that your variable is a void pointer is irrelevant.

I suggest looking for register .

+1
source share

The storage register cannot be applied to pointers. Like a register placed in a machine register.

0
source share

All Articles