Visualizing the C++ Object Memory Layout Part 1: Single Inheritance

A follow-along series of coding experiments that allowed me to explore and understand concepts like the vptr, vtable, and how the compiler organizes base and derived members in memory by seeing them in action through code.

Sofía Belén López Vicens
This is a great post! I love seeing stuff like this. It also illustrates one of the big differences between #cpp and #rustlang: Because Base has a virtual function, each Base or Derived object includes a hidden pointer to the vtable (vptr), typically the first word in the object layout.
With #rustlang's traits, they're implemented as a "fat" pointer: a (pointer to vtable, pointer to data), whereas a pointer to a polymorphic object in #cpp is a thin pointer, and then the object itself contains the pointer to the vtable, with the data following (as I just quoted).
Both of these options are useful in different situations: #rustlang's is more flexible, but can use more memory, since the pointers are twice as wide. A vector of trait objects in Rust is double the size of a vector of polymorphic objects in #cpp.
But #rustlang can implement a trait for a type in another package, whereas you can't do that with #cpp inheritance in the same way.
I'll point out that the #cpp way is useful enough that some #rustlang packages emulate it with unsafe code: anyhow being a major example. You want errors to be as small as possible, and so the single vs double pointer matters.