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?
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.
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.
In the C programming language, register is a reserved word (or keyword), type modifier, storage class, and hint.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With