All Topics
All Topics
Technology
Technology
Design
Design
Programming
Programming
Science
Science
News
News
Gaming
Gaming
Entertainment
Entertainment
Business
Business
Finance
Finance
Sports
Sports
Health
Health
Food
Food
Travel
Travel
Art
Art
Music
Music
Books
Books
Education
Education
Politics
Politics
Personal
Personal
No algorithm. No AI slop. No ads. Just RSS. Pro-human. Indie writers. Real journalism. Open web. Chronological. Hand toasted.

Pointer Tagging Techniques in C++: Utilizing Unused Bits for Memory Optimization

By

signa11

8mo ago· 7 min readen

Summary

This technical article explores pointer tagging in C++ programming, explaining how modern 64-bit systems leave unused bits in pointers that can be repurposed to store metadata. The technique leverages the fact that most systems only use 48 bits for virtual addresses and malloc implementations align allocations to 16-byte boundaries, leaving both upper and lower bits available for tagging. Pointer tagging enables memory savings, faster dynamic dispatch, and more compact data structures by storing type information, flags, or other metadata directly within pointer values.

Key quotes

· 4 pulled
A 64-bit pointer can address over 18 exabytes (18 billion gigabytes) of memory, which far exceeds the needs of even the most top-end supercomputers
Most modern desktop CPUs only use 48 bits for virtual addresses, leaving the upper 16 bits unused
Most malloc implementations align allocations to 16-byte boundaries, so the bottom four bits are always set to zero
These bottom four bits can be repurposed to store extra data, as long as those bits are cleared back to zero when dereferencing the pointer
Snippet from the RSS feed
Using tagged pointers to save memory, speed up dynamic dispatch, and compact data structures

You might also wanna read