Get the latest tech news

Pointer Tagging in C++: The Art of Packing Bits into a Pointer


Using tagged pointers to save memory, speed up dynamic dispatch, and compact data structures

For this article, we’ll assume a 48-bit canonical address space (though keep in mind that this detail is platform-dependent), giving us 16 high bits to work with. At run-time, a switch statement casts the pointer to the correct type before calling the corresponding “virtual” function — not far off from what was shown in this article! These spare bits can be repurposed to store additional information, enabling more compact data layouts and reducing the cost of run-time polymorphism.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Art

Art

Photo of C++

C++

Photo of pointer tagging

pointer tagging

Related news:

News photo

Bezier Curve as Easing Function in C++

News photo

In Defense of C++

News photo

Rust-style safety model for C++ 'rejected' as profiles take priority