int arr[] = {1, 2, 3, 4};
// Memory layout:
// Index: 0 1 2 3
// Value: [1][2][3][4]
// LB = 0 (Lower Bound), UB = 3 (Upper Bound)
address_of(arr[i]) = Base(arr) + i * sizeof(element)
Where:
Base(arr) → Starting address of arraysizeof(element) → Size of each element in bytesi → Index of element to accessLB → Lower bound (always 0 in C++)UB → Upper bound (n-1 where n is array size)In C++,array do not track how many elements are logically filled.
size = sizeof(arr)/sizeof(arr[i])
//Commonly used formula To find filled elems in a array.
//But this is only useful for finding total memory blocks present in a array.
//i.e gives the **total capacity**, not the count of meaningful elements.
The compiler translates arr[i] to *(Base(arr) + sizeof(element) * i)
Example:
Base address = 104, sizeof(int) = 4, index = 6
Address = 104 + (4 * 6) = 104 + 24 = 128