Search code examples
arrayscompiler-construction

How does compiler fetch elements from array of objects in constant time?


Whenever we want to fetch elements based on index from an array, I learned that the compiler just does something like this to fetch the element in constant time:

array_addr + ele_size * (i - first_index) (where the ele_size depends on the type of the elements present in an array, e.g. for array[int] the ele_size will be 4 bytes)

So how does compiler fetch elements from array[object] in constant time, when each object could have a different size (in memory)?

PS: Question is not specific to any language


Solution

  • array[object] and array[int] are actually very similar, since an array of objects doesn't usually store the actual objects directly next to each other like this:

    // [ _______Person________,  _____Pet_____,  _______Person_______ ]
       [ Name,    Age, Country,  Name,    Type,  Name,   Age, Country ]
       [ "Lucas", 37,  FRANCE,   "Misty", CAT,   "Emma", 22,  SWEDEN  ]    
    

    Instead, the array just stores references (pointers to the objects), which are all of the same size (32 or 64-bit int):

    // [ ______Person______,  ________Pet_______,  ______Person______ ]
       [ address of Person,   address of Pet,      address of Person  ]
       [ 0xaba11b8ae55fa4d2,  0xb8e4f4adea6be036,  0x5ce415a69ca50222 ]
    

    Then the data for each object is found at the specific memory address and can occupy however many bytes is needed.

    You might find this question about how object[] is stored in memory (in C#) useful.