I Was confused for how we get the address and address calculation done in an array when we get element from array for passing an index.
So, any data structure used to achieve this mechanism? is there any mapping in memory or hashing or indexing like used in DB?
It is simple, just like mathematical formula.
Consider one dim array ARR of N integer(2 byte memory for each element) stored in memory.
Now, to find address of ARR[i]:
Adddress(ARR[i]) = Base Address + No. Of bytes for each element * (Find index - Lower bound of ARR)
Here,
Base Address = starting address of an array in memory
No. Of bytes for each element = for integer, it is 2 bytes
Find index = which element you are searching in an array
Lower bound of ARR = lower bound index of array
For Ex. consider integer array int[] ARR = new int[4];
So, any data structure used to achieve this mechanism? is there any mapping in memory or hashing or indexing like used in DB?
It is simple, just like mathematical formula.
Consider one dim array ARR of N integer(2 byte memory for each element) stored in memory.
Now, to find address of ARR[i]:
Adddress(ARR[i]) = Base Address + No. Of bytes for each element * (Find index - Lower bound of ARR)
Here,
Base Address = starting address of an array in memory
No. Of bytes for each element = for integer, it is 2 bytes
Find index = which element you are searching in an array
Lower bound of ARR = lower bound index of array
For Ex. consider integer array int[] ARR = new int[4];
Address | Value | Element |
100 | 11 | 1st |
102 | 12 | 2nd |
104 | 13 | 3rd |
106 | 14 | 4th |
As per above table,
Addresss(ARR[2]) = 100 + 2* (2-0) = 100+4=104
Happy Learning!!!
No comments:
Post a Comment