I want to know, How do we make sure if a variable defined with register specifier got stored in CPU register?
How do make sure if a variable defined with "register" specifier got stored in CPU register?
2k views Asked by msc AtThere are 6 answers
On
Disassemble the code and check. It may not really be clear at that point, because variables don't really exist, they're just names that link producers with consumers. So, there is not necessarily a register reserved for that variable - maybe it disappeared entirely, maybe it lives in several registers over its lifetime, maybe none of the above.
On
Historically, the register keyword was introduced decades ago as an optimization hint to the compiler. Nowadays, when the processors have more general purpose registers, the compiler usually places variables in registers even without being told so (when the code is compiled with optimizations).
Being just a hint and not an enforcement, you cannot do anything to force it. You can, however, write that part of the code in assembler. This way you have complete control of where your variables are stored.
On
Maybe calling Assembly instructions will help with it:
/// Function must be something like this:
int check_register_storing()
{
__asm__ (
pushad // Save registers
and ebx, ebx // Set Zero
and eax, eax
and ecx, ecx
and edx, edx
);
// Set test number.
register int a = 8; // Initial value;
int from_register = 0;
asm(
add eax, ebx // If, 'a' variable set on CPU register,
add eax, ecx // Some of main usage registers must contain 8
add eax, edx // Others must contain 0
mov %from_register, eax
popad // Return default parameters to registers
}
/// Check result
printf( "Original saved number: %d, Returned number from main registers: %d\n", a, from_register );
}
On
If the variable is stored in a register means it is not stored in memory. So, the bull's eye is try to access the address of the variable using printf. If the output gives some address, conclusion is it is stored in memory so it would act as auto storage class variable (and it is not stored in register). But if it gives error "incompatible implicit declaration of built-in function 'printf' "..this means the variable is stored in register and would behave as register storage class variable..
On
I don't know if I am wrong or right, but we know that a normal variable is stored in memory which has address, but we know that if we write register int a; then a register may be allocated for the variable, but we know that registers have names, not address, so we can't assign pointer to point to a registers because pointers stores address only, so if we write as follow:-
#include<stdio.h>
int main()
{
register int reg = 5;
int *p = ®
printf("%d",reg);
}
then it should give error as: "address of register variable ‘reg’ requested" if the register has successfully allocated to our variable, and if register is not allocated then memory addres can be assigned to pointer hence no error should be there. IMPORTANT:-This is my first answer on stackoverflow, I may be wrong, please correct me if i am, I'm still learning.
Basically, you cannot. There is absolutely nothing in the C standard that gives you the control.
Using the
registerkeyword 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)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.