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.
An array in C or C++ can be placed in a register explicitly if it is allocated memory statically and its memory is not accessed.
Register array consists of registers identified by letters like B, C, D, E, H, L and accumulator. The control unit controls the flow of data and instructions within the computer.
The instruction read from memory is placed in the Instruction register (IR). The Temporary Register (TR) is used for holding the temporary data during the processing. The Input Registers (IR) holds the input characters given by the user. The Output Registers (OR) holds the output after processing the input data.
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. :-)
Don't mix register
as a keyword with CPU registers. Your code
register int arr[4];
makes that arr
is completely inaccessible, since you can't take the address of the object. Basically the only thing that you can do with it is sizeof arr
.
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