In below code,
int main( )
{
register int arr[4];
/* ... */
}
Is it possible that 'arr' is allocated in some cpu register. (Consider cpu has 4 or more registers).
Or compiler will ignore register storage class for array.
As per my understanding, answer is YES and NO.
NO because,
Any array element must be explicitly addressable (i.e. for eg. for 16 bit uC/uP its address should always lie between 0x0000 to 0xFFFF address space.)
CPU registers are accessed using register direct addressing mode ( such as mov r2,#100 ). This addressing mode does not have an effective address. ( even it is not considered to be an addressing mode )
Array elements must reside in continous memory locations. ( for pointer arithmetic, the main reason to use array )
and YES because,
See below code.
int main( )
{
register int arr[4];
int i;
arr[0] = 10; /* OK */
arr[1] = 20; /* OK */
arr[2] = 30; /* OK */
arr[3] = 40; /* OK */
for(i=0;i<4;i++)
arr[i]=10; /* Error : "address of register variable 'arr' requested" */
return 0;
}
So my final conclusion is that, ideally register storage class should never be used with array even if your compiler permits it.
Please correct me or give more inputs. :-)