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 the register specifier.
6.7.1
The implementation may treat any register declaration simply as an auto declaration. However, whether or not addressable storage is actually used, the address of any part of an object declared with storage-class specifier register cannot be computed, either explicitly (by use of the unary & operator as discussed in 6.5.3.2) or implicitly (by converting an array name to a pointer as discussed in 6.3.2.1).
And 6.5.3.2:
The operand of the unary & operator shall be either a function designator, the result of a [] or unary * operator, or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier.
As for C++, if you use & with a register, it cancels its meaning:
ANSI C does not allow for taking the address of a register object; this restriction does not apply to C++. However, if the address-of operator (&) is used on an object, the compiler must put the object in a location for which an address can be represented. In practice, this means in memory instead of in a register.
you are right, if you use & to a register variable, that make this variable to main memory, so you can get it's address. because register variable have not address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With