Is it possible to create a type in c++ that takes less than one byte of memory?

Not really. Inside a struct, you can make use of bit fields. So if you know you’ll need a certain, fixed amount of entries, this would be a way to save a few bits (but note that the struct will always be padded to at least the next whole amount of bytes). Also note that due to the fact that “normal” CPUs can’t address amounts smaller than an octet/byte, the access to these bit field values might be slower because of the extra instructions the compiler has to generate to get/store a value “in the middle”. So in order to save a few bits, you have to spend some CPU time.

The C++11 standard says in chapter 1.7 The C++ memory model (emphasis mine):

The fundamental storage unit in the C++ memory model is the byte. A byte is at least large enough to contain any member of the basic execution character set (2.3) and the eight-bit code units of the Unicode UTF-8 encoding form and is composed of a contiguous sequence of bits, the number of which is implementation- defined.

In other words: the smallest addressable unit in C++ is at least 8 bits wide.

Side-note: In case you’re wondering: there are machines like DSPs that can only address units larger than 8 bits at a time; for such a machine, the compiler may define “byte” to be, for example, 16 bits wide.

Leave a Comment