<p>In the normal implementations of C++, while not guaranteed by the standard, <a href="https://stackoverflow.com/a/27682344/3998030">there is a vtable pointer as the header of every inherited class that needs a vtable pointer</a>. There will be multiple vtable pointers for multiple inheritance that all have virtual functions.</p>
<p>In Java and C#, <a href="https://softwareengineering.stackexchange.com/a/373810/181279">there is one single vtable pointer in each object, and vtables for interfaces are looked up in that main vtable</a>. That basically means there could not be two instances of the same interface in a single object.</p>
<p>The other way that is easy to think is to use fat pointers. The vtables are stored in pointers. And casting are done by modifying the vtable in the pointer.</p>
<p>They all have pros and cons. But are there actual examples implementing the 3rd option?</p>

<memory-management><pointers><inheritance>
Are there actual languages using fat pointers to store types?
CC BY-SA 4.0
2024-07-17T08:52:37.547Z
conceptually you can consider the function pointer+context pointer pattern of C API callbacks to be a fat pointer.
ratchet freak
CC BY-SA 4.0
2024-07-17T18:49:47.017Z
Rust's [trait objects](https://doc.rust-lang.org/reference/types/trait-object.html) (`dyn Trait`). Specifically, a pointer to a trait object is a fat pointer. This guide ["Rust Deep Dive: Borked Vtables and Barking Cats"](https://geo-ant.github.io/blog/2023/rust-dyn-trait-objects-fat-pointers/) provides a good explanation of how it works.
tarzh
CC BY-SA 4.0
2024-07-17T21:38:11.493Z
@MooingDuck They have a subset of multiple inheritance, that is on interfaces. In C++ you need more vtable pointers for things that are conceptually just interfaces.
user23013
CC BY-SA 4.0
2024-07-17T23:16:01.350Z
@user23013: You're right, I was mistaken.
Mooing Duck
<p>As far as I know, Go does this with dynamically allocated vtables for all types that are static interfaces. Even if the static type isn't a pointer to an interface (causing the rumors that interface pointers are an error, but that's not really the question). Also, reality in Go is slightly more complex, because they sometimes optimize the pointer part in the fat pointer and store the target value directly, e.g. for int.</p>
<p>From my understanding, this is required if interfaces are commonly used and not declared at compile time.</p>

CC BY-SA 4.0
2024-07-27T11:43:01.157Z
2024-07-27T11:43:01.157Z
feldentm
<h1>Swift protocol existentials</h1>
<p>When you use a &quot;protocol existential&quot; type like <code>any CustomDebugStringConvertible</code>, it stores the vtable alongside the value like you describe. Technically this is five words and not just two, but there's a reason for this.</p>
<p>The first three words are the value itself if it's small enough, or a pointer to the value if not. There's a part of a Swift ABI document that explains why 3 is the magic number, but my understanding is that it's so that the traditional pointer/length/capacity implementation of dynamic arrays fits inline.</p>
<p>The fourth word is the &quot;value witness table&quot; and contains basic operations that every type has, like copy and deinit, where the latter is a no-op for primitive types like <code>Int</code>. The fifth word is the &quot;protocol witness table&quot; which is the standard vtable.</p>
<p><code>Any</code> is the same general shape, but without the protocol witness table, so it's only four words total.</p>

CC BY-SA 4.0
2024-07-17T13:24:15.290Z
2024-07-17T13:24:15.290Z
Bbrk24
# Are there actual languages using fat pointers to store types?
Posted by: username_0 on 2024-07-17
In the normal implementations of C++, while not guaranteed by the standard, [there is a vtable pointer as the header of every inherited class that needs a vtable pointer](https://stackoverflow.com/a/27682344/3998030). There will be multiple vtable pointers for multiple inheritance that all have virtual functions.

In Java and C#, [there is one single vtable pointer in each object, and vtables for interfaces are looked up in that main vtable](https://softwareengineering.stackexchange.com/a/373810/181279). That basically means there could not be two instances of the same interface in a single object.

The other way that is easy to think is to use fat pointers. The vtables are stored in pointers. And casting are done by modifying the vtable in the pointer.

They all have pros and cons. But are there actual examples implementing the 3rd option?
**Tags:** memory-management pointers inheritance
> **Comments:**
> **username_1**: conceptually you can consider the function pointer+context pointer pattern of C API callbacks to be a fat pointer.  
> **username_2**: Rust's [trait objects](https://doc.rust-lang.org/reference/types/trait-object.html) (`dyn Trait`). Specifically, a pointer to a trait object is a fat pointer. This guide ["Rust Deep Dive: Borked Vtables and Barking Cats"](https://geo-ant.github.io/blog/2023/rust-dyn-trait-objects-fat-pointers/) provides a good explanation of how it works.  
> **username_0**: @MooingDuck They have a subset of multiple inheritance, that is on interfaces. In C++ you need more vtable pointers for things that are conceptually just interfaces.  
> **username_3**: @user23013: You're right, I was mistaken.  
---
## 2 answers
### Answer by username_5 on 2024-07-17. Score: 3
# Swift protocol existentials

When you use a "protocol existential" type like `any CustomDebugStringConvertible`, it stores the vtable alongside the value like you describe. Technically this is five words and not just two, but there's a reason for this.

The first three words are the value itself if it's small enough, or a pointer to the value if not. There's a part of a Swift ABI document that explains why 3 is the magic number, but my understanding is that it's so that the traditional pointer/length/capacity implementation of dynamic arrays fits inline.

The fourth word is the "value witness table" and contains basic operations that every type has, like copy and deinit, where the latter is a no-op for primitive types like `Int`. The fifth word is the "protocol witness table" which is the standard vtable.

`Any` is the same general shape, but without the protocol witness table, so it's only four words total.
---
### Answer by username_4 on 2024-07-27. Score: 0
As far as I know, Go does this with dynamically allocated vtables for all types that are static interfaces. Even if the static type isn't a pointer to an interface (causing the rumors that interface pointers are an error, but that's not really the question). Also, reality in Go is slightly more complex, because they sometimes optimize the pointer part in the fat pointer and store the target value directly, e.g. for int.

From my understanding, this is required if interfaces are commonly used and not declared at compile time.
---
