Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do make sure if a variable defined with "register" specifier got stored in CPU register?

I want to know, How do we make sure if a variable defined with register specifier got stored in CPU register?

like image 636
msc Avatar asked Apr 29 '16 11:04

msc


1 Answers

Basically, you cannot. There is absolutely nothing in the C standard that gives you the control.

Using the register keyword is giving the compiler a hint that that the variable maybe stored into a register (i.e., allowed fastest possible access). Compiler is free to ignore it. Each compiler can have a different way of accepting/rejecting the hint.

Quoting C11, chapter §6.7.1, (emphasis mine)

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.

FWIW, most modern-day compilers can detect the mostly-used variables and allocate them in actual register, if required. Remember, CPU register is a scarce resource.

like image 155
Sourav Ghosh Avatar answered Oct 04 '22 20:10

Sourav Ghosh