sizeof(Derived)
is greater than sizeof(Base)
. That's the reason.
Generally, indexing an array of Foo
objects at index i
works the following way:
element_address = array_base_address + i * sizeof(Foo)
You can see how this indexing breaks if the array elements are not of expected size. Why it works for some indices is because sometimes the calculated element address points to a valid object in memory (but it's not actually the i
th object).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…