I understand that a register keyword will assign a register for computing the value and a volatile keyword will read the value from the memory every time when we perform some computation on the variable and basically not optimize the code. So, if a variable is assigned both these keywords then, would it mean that it would essentially be volatile itself? I cannot understand the behavior by writing a sample code. Can anybody shed some light?
I understand that a register keyword will assign a register for computing the value
Share
In C, the
registerstorage class behaves exactly as theautostorage class, except that the implementation is required (per 5.1.1.3) to issue a diagnostic if the program attempts to take or use the address of the object (6.5.3.2, 6.7.1). The use ofregisteras an optimisation hint to the compiler is generally pointless, as a compiler smart enough to exploit the non-stored quality of the object is certainly smart enough to track which objects could be declaredregister; instead, it should be understood as a code quality check that the programmer is not inadvertently destroying optimisation opportunities by taking the address of the object.In other words, removing all instances of the
registerkeyword from a valid program has no effect on the program’s semantics; it is similar in this regard tostatic_assert.The
volatiletype qualifier indicates that accesses to (reads from and writes to) the object are considered side effects and cannot be optimised away. For an object that does not exist in a defined location memory (i.e., one having theregisterstorage class), this would be most useful in performance testing: