6

Should I use the register keyword on my phone variable not? I have this:

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

   if (phone)
   {
       return phone;
   }
   return NULL;
}

Is there a difference? What should I do?

Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Tim
  • 85
  • 3
  • 7
    The `register` keyword is completely obsolete for its original purpose. The only potential use I can think of for it is if you want the compiler to generate an error if anyone tries to take the address of the variable. – R.. GitHub STOP HELPING ICE Mar 31 '11 at 23:56

6 Answers6

12

The register keyword was intended as an optimization hint to the compiler. The problem is, the compiler knows your code better than you do, and these days do not need such simplistic hints to generate better code.

So the only thing register does with modern compilers is prevent you from using & to take the address of the variable. That's it.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 2
    It's still quite useful for some embedded systems, for example, if you need sub-microsecond accuracy (or exact number of cpu cycles) in your timed events, and are using inline assembly in your C code. In a recent project the `register` keyword allowed me to write 5% of the code in assembly and 95% comfortably in C. Without it, I would have needed to write 100% of it in assembly. – vsz Jan 22 '14 at 17:41
  • @vsz, interesting, I haven't seen a case of `register` leading to performance improvements in a long time. – sarnold Jan 22 '14 at 21:58
  • in my case it wasn't even about all-out performance. It was about performance at very specific points, when in case of signal generation, the exact amount of clock cycles have to pass between setting the outputs. Fortunately, for good embedded compilers, the `register` keyword in not just a hint, it's a strict command... – vsz Jan 23 '14 at 07:13
  • This way, for example, I can use inline assembly in time-critical interrupts or similar situations, and still be able to use C for the rest of the code (and the same variables in both C and assembly), without worrying that the program will start pushing and popping registers (or corrupting variables if I don't push and pop them) because they were used for something else. – vsz Jan 23 '14 at 07:14
5

There's 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 variable will be heavily used and that it should be put into a CPU register.

Still, in general there's no reason to use register anywhere, since optimizers included in current compilers are much better than programmers at guessing what variables should be put into registers, and almost always will happily ignore the register keyword completely.

The C99 standard also provides some additional details about register (§6.7.1 ¶4):

A declaration of an identifier for an object with storage-class specifier register suggests that access to the object be as fast as possible. The extent to which such suggestions are effective is implementation-defined. 101)

101) The implementation may treat any register declaration simply as an auto declaration. However, whether or not addressable storage is actually used, the address of any part of an object declared with storage-class specifier register cannot be computed, either explicitly (by use of the unary & operator as discussed in 6.5.3.2) or implicitly (by converting an array name to a pointer as discussed in 6.3.2.1). Thus, the only operator that can be applied to an array declared with storage-class specifier register is sizeof.

So using register you most probably get only the downsides of pretending having a variable in a register. :) Notice that the C++ standard relaxes these restrictions, but obviously if you try to take the address of a register variable it won't stay in a register anymore.

Long story short, register is there mostly as a relic of the past, avoiding it is the best use you can do of it. :)

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
3

The register keyword tells the compiler you want that variable stored in a CPU register as opposed to system RAM. In general this is a BAD idea, most compiler optimizers can do a much better job of handling variables, and many compilers ignore the keyword altogether.

Just remove it.

Unsigned
  • 9,640
  • 4
  • 43
  • 72
2

As far as I remember, the register keyword tells the compiler to try to save the variable in a register of the CPU, for increasing the principle of locality. The programmer knows that the variable will be accessed many times so he wants to keep it in the fastest memory location that he can obtain.

I suspect that many compilers today are smarter than (the average) programmers when it comes to this kind of optimizations, so it could be not needed. I would not use it.

Andrea Spadaccini
  • 12,378
  • 5
  • 40
  • 54
1

It sounds like you shouldn't be using registers at all.

Registers are directions to compilers of where to put variables. They don't have types. The fact that your variable is a void pointer is unrelated.

I suggest looking up register.

Community
  • 1
  • 1
usul
  • 784
  • 5
  • 16
0

register storage variable can't be applied to pointers. As register varible placed in machine register.

Laxman N
  • 1
  • 2