In C, does usage of a pointer cancel the “register” property of the associated variable?
#include<stdio.h>
#include<stdlib.h>
int main()
{
register int clk=0; //maybe register maybe not
int *adr=&clk; //not a register now? i have its address
*adr=1; //if i use this 1000000 times, does it exist in L1 at least?
printf("%d",clk);
return 0;
}
Gives compiler error “can’t take address of register variable” but it is not register %100. it is only a chance.
Is this the slowest loop?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
int i=0;
p=&i;
for(*p=0;(*p)<100;(*p)++)
{
//do nothing
}
printf("%d ",i);
return 0;
}
If I make nearly all variables pointer-style and only three variables only primitive type with “register” keyword, does compiler make those three variables “really register” with a higher chance?
OK. Problem solved. I learned some assembly and found out that this depends on optimization level and also variable’s volatility. Using __asm{} makes sure it computes in a register.
Thanks.
In C it’s illegal to apply
&to a variable declared with theregisterspecifier.And 6.5.3.2:
As for C++, if you use & with a
register, it cancels its meaning: