Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use the register keyword in my code?

Tags:

c

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?

like image 203
Tim Avatar asked Mar 31 '11 23:03

Tim


People also ask

What is the purpose of register keyword?

Register keyword tells compiler to store the particular variable in CPU registers so that it could be accessible fast. From a programmer's point of view register keyword is used for the variables which are heavily used in a program, so that compiler can speedup the code.

Why register keyword is used in C?

Registers are faster than memory to access, so the variables which are most frequently used in a C program can be put in registers using register keyword. The keyword register hints to compiler that a given variable can be put in a register.

Is register a keyword?

In the C programming language, register is a reserved word (or keyword), type modifier, storage class, and hint.

What is the right semantics of using the register keyword?

Register variables tell the compiler to store the variable in CPU register instead of memory. Frequently used variables are kept in registers and they have faster accessibility. We can never get the addresses of these variables.


1 Answers

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.

like image 138
sarnold Avatar answered Nov 11 '22 05:11

sarnold