memory - How is array element located without array iterating? -


it said array element access quick in programming language knowing numerical index of element. assume because computer memory kind of array each memory slot having numerical address. so, knowing memory address can go directly address , access value stored there.

how memory location accessed quickly? done @ level of microcode?

the local variable references memory location of first element in array. elements in array of fixed size (such 4 bytes each integer) , stored in order in contiguous block of memory. calculation memory location of element memory location of first element + (size in bits of single element * index of element) single dimensional array. note index used in calculation zero-based, why languages use zero-based indices arrays. elements of array might references objects keep element sizes fixed, rather object might of variable size.

explanation multidimensional array: element locations in multidimensional array can calculated using location of element [0][0] + (size of single element * (index 1st dimension + (index 2nd dimension * length of 1st dimension) + ... + (index nth dimension * length of (n-1)th dimension * ... * length of 1st dimension))) in column major system. there 2 systems, column major , row major. look @ page visualization math still rather simple computer though looks complicated here. multidimensional arrays implemented array of arrays, slower , less memory efficient.

out of bounds checking: calculation element position not account indices being larger length of array, can lead accessing memory locations outside array. c language not prevent this. java throw outofboundsexception, accessing array becomes more expensive due check being performed. out of bounds checking language specific, if it's implemented


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -