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?
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.
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 anauto
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 specifierregister
issizeof
.
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. :)
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.
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.
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 .
register storage variable can't be applied to pointers. As register varible placed in machine register.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.