c++ - Why can't we always use the register storage class in C? -


i read in book that, whenever declare variable storage class register, stored in register, subject availability. if no register available, default storage type of 'auto' assigned it.

whenever declare variable without explicitly mentioning storage class, default storage type assigned 'auto' itself.

so, question is, why not declare every variable of 'register' storage class - if no register available, anyways treated default 'auto' class itself. , luckily, if registers available, stored in one. understand cannot use & operator longer, if i'm not going work pointers , addresses? can declare variables 'register' storage class then? because seems bad practise.

edit: searched web, 'unavailability of address' point mentioned. why can't rest variables declared 'register' not mentioned.

you cannot make variables register, because c (and (c++) language specification(s) explicitly forbids taking address of register variable.

however, the register qualifier not having role in today's optimizing compilers gcc or clang/llvm , these compilers happily , freely use machine register variables not qualified register or keep in memory (not in machine register) variable qualified register. the compiler ignoring register qualifier (except forbid taking address). has complex register allocation algorithms , heuristics. given variable might stay in machine register parts of function code, , put in memory other parts.

from optimization point of view, current compilers handle auto , register qualified variables in same way (hence register qualifier useless, except forbid address-of operator).

notice the cpu cache more important processor registers today. if want hand-tune c code performance (which bad idea, since compiler doing better can), better take care of caching issues (see this).

afaik, future versions of c , c++ languages officially deprecate register qualifier (as did auto qualifier), , happen future language specifications reuse keyword other purposes (as c++11 reused auto). using register in source code mistake, since might make code harder port future versions of c or c++.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -