OOP Under the Hood
Classes & Object-Oriented Programming · Coding Fundamentals track
7 min read
In this module
How Objects Really Work
A line like my_dog = new Dog("Rex", 3) looks innocent. Underneath that high-level syntax sits a fair amount of low-level machinery. Pulling it apart connects your OOP knowledge to the hardware concepts you've been studying.
Object Layout in Memory
An object is a contiguous block of fields in memory, much like a C struct. When Dog extends Animal, the Dog object lays out the Animal fields first, followed by the Dog-specific fields after them.
class Animal { name // 8 bytes (pointer to string) age // 4 bytes (integer)}
class Dog extends Animal { breed // 8 bytes (pointer to string)}The Animal fields (name, age) come first, with the Dog field (breed) tacked on after. The payoff: a pointer to a Dog also works as a pointer to an Animal, because the Animal fields land at the same offsets either way. That's polymorphism at the memory level.
Objects are laid out in memory with parent class fields first, followed by child class fields. This ensures that a pointer to a child object is also a valid pointer to the parent type - the foundation of runtime polymorphism.
That's the simple picture for single inheritance. With multiple inheritance, C++ sometimes has to adjust the pointer when it converts to a second base class, because that base can't also sit at offset 0. The diagram also leaves out padding. Here's the same layout as C structs; before running it, predict the offset of breed and the size of each struct.
age takes 4 bytes, but breed is a pointer that must start on an 8-byte boundary, so the compiler leaves 4 unused bytes after age: 8 + 8 + 4 + 4 of padding, then breed at offset 24.
The Virtual Table (vtable)
Calling a virtual (overridable) method puts the CPU in a tricky spot: it has to figure out which function to actually execute at runtime. The mechanism is the vtable (virtual method table).
Every class with virtual methods has a vtable, a static array of function pointers. Each object carries a hidden pointer to its class's vtable. Calling a virtual method, the CPU:
- Follows the object's vtable pointer.
- Indexes into the vtable to find the right method.
- Calls the function at that address.
That's the C++ model, and Java's virtual machines work much the same way. Dynamic languages such as Python and JavaScript look methods up by name at runtime instead (in the class's attribute dictionary in Python, along the prototype chain in JavaScript), with caches to keep it quick.
That extra indirection (pointer to vtable, index into vtable, then call) is the cost of polymorphism. Two pointer dereferences, blink and you'll miss them. The bigger price is that the compiler can't inline the call, which sometimes matters in performance-critical code.
Here's the same machinery in C++ terms, down to the x86-64 instructions. Call speak() or eat() and step through the two loads, then flip the object between Animal and Dog. The instructions never change; only the vptr does.
Virtual call through a Animal*
Pick a method to call through a Animal*, then step through the loads. Switch the object's type and only the vptr changes. Addresses are illustrative; the layout follows the Itanium C++ ABI on x86-64.
rdi holds the object's address, 0x4052a0. The pointer's type is Animal*, so this code has no idea the object is a Dog.
x86-64 (Intel syntax)
- ; rdi = this (type Animal*), set by the caller
- mov rax, QWORD PTR [rdi] ; load the vptr
- mov rax, QWORD PTR [rax] ; load slot 0 (speak)
- call rax ; indirect call
Registers
- rdi
- 0x4052a0
- rax
- -
- target
- -
Dog object at 0x4052a0 (32 bytes)
| +0 | vptr | 0x403d50 → Dog vtable |
| +8 | name (const char*) | 0x402010 → "Rex" |
| +16 | age (int) | 3 |
| +20 | padding | 4 bytes |
| +24 | breed (const char*) | 0x402014 → "Lab" |
Functions in .text
- 0x401140 Animal::speak
- 0x401170 Animal::eat
- 0x4011a0 Dog::speak
vtable for Animal (_ZTV6Animal at 0x403d20)
| -16 | offset-to-top | 0 |
| -8 | typeinfo | 0x403f00 (typeinfo for Animal) |
| +0 | [0] speak | 0x401140 Animal::speak |
| +8 | [1] eat | 0x401170 Animal::eat |
vtable for Dog (_ZTV3Dog at 0x403d40)
| -16 | offset-to-top | 0 |
| -8 | typeinfo | 0x403f20 (typeinfo for Dog) |
| +0 | [0] speak | 0x4011a0 Dog::speak ← vptr |
| +8 | [1] eat | 0x401170 Animal::eat (inherited) |
Object Allocation
Calling new Dog(...) kicks off this sequence in the runtime:
- Calculate the total size needed (all fields plus the vtable pointer).
- Allocate that many bytes on the heap (via
mallocor the language's allocator). - Set the vtable pointer to the Dog class's vtable.
- Call the constructor to initialise the fields.
- Return a pointer (reference) to the allocated memory.
The variable my_dog holds a pointer to the object on the heap. That's why in many languages, assigning one object variable to another copies the pointer and leaves the object where it is.
dog_a = new Dog("Rex", 3, "Lab")dog_b = dog_a // dog_b points to the SAME object
dog_b.name = "Changed"print(dog_a.name) // "Changed" - same object!Method Dispatch: Static vs. Dynamic
Static dispatch (non-virtual methods) gets resolved at compile time. The compiler knows the exact function to call and emits a direct CALL instruction.
Dynamic dispatch (virtual methods) gets resolved at runtime through the vtable. The compiler emits code that loads the vtable pointer, looks up the method, and calls through the pointer.
// Static dispatch - compiler knows exactly which function to callobject.non_virtual_method()// Compiles to: CALL 0x3000
// Dynamic dispatch - resolved at runtimeobject.virtual_method()// Compiles to:// LOAD vtable_ptr from object// LOAD method_ptr from vtable[index]// CALL method_ptrThe performance difference is usually negligible. In hot loops with millions of iterations, though, the inability to inline virtual calls can sting. That's why some languages (C++ with final, Rust with monomorphisation) give you ways to opt out of virtual dispatch when you don't need it.
Destructors and Memory Management
Once an object is no longer needed, its memory has to be reclaimed. In languages with manual memory management (C++), you call delete, which invokes the destructor — a special method that cleans up resources (closing files, freeing memory) before the object's memory goes back to the heap.
In garbage-collected languages (Java, Python, C#), the runtime detects when objects have no references left and reclaims their memory automatically. Safer than manual management, with the cost of the collector's periodic scanning.
The Bigger Picture
OOP is an abstraction layer over raw memory manipulation. Classes are structured memory layouts. Methods are functions with an implicit this pointer. Inheritance is memory layout compatibility. Polymorphism is function pointer tables. Knowing these mappings doesn't diminish OOP. It sharpens your sense of what the machine is actually doing when you write object-oriented code.
This knowledge pays off the moment you start studying reverse engineering and exploitation. When you spot a vtable pointer being overwritten in a use-after-free exploit, you'll know exactly why that hands an attacker control over program execution.
You can build the whole mechanism by hand in C: a table of function pointers per "class", and a pointer to it in each object. The last few lines of main play the attacker. Real vtables live in read-only memory, so exploits overwrite the object's vtable pointer to aim it at a fake table, exactly as this does.