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

For my computation I only need to use 7-bit space so I am using a char type. However I wonder if it is possible to declare my own type that uses less than one byte of memory?


Solution 1:

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.

Solution 2:

Even if you try to create a bitset with size 1, it will consume a byte. So it is not possible to create a type less than a byte in C++

#include <iostream>       // std::cout
#include <string>         // std::string
#include <bitset>         // std::bitset

int main ()
{
  std::bitset<1> foo;
  std::bitset<1> bar (1);
  std::bitset<1> baz (std::string("1"));

  std::cout << "foo: " << sizeof(foo) << '\n';
  std::cout << "bar: " << sizeof bar << '\n';
  std::cout << "baz: " << sizeof baz << '\n';

  return 0;
}

Solution 3:

byte is the minimal addressable unit and has the minimum size in C++ equal to 1. And the memory is allocated in bytes. Even an empty class has at least size not less than 1 byte.

Solution 4:

Assuming the processor isn't some special hardware completely different from regular hardware, you won't gain anything from using 7 instead of 8 bits, since an 8-bit byte is the minimum addressable unit (aside from a few exotic machines that use more than 8 bits to a byte, and in my 30+ years of using computers, I have not worked with one except possibly the Honeywell mainframe that a friend of mine worked during his summer internship which and I was invited to visit for a day - I don't know what model it was, so it may have been a 18- or 36-bit machine - and that was about 1985 or so).

So, assuming you could make a big array of 7-byte "lumps", the compiler would have to generate extra code to extract each 7-byte element from the array, so instead of taking one instruction to fetch one 8-bit byte, all accesses will be at least two operations, and some will straddle two machine-words, requiring two more operations to extract the relevant combination of 7 bits out of the two machine words. It is possible to do, but it would most likely waste more than the 1/8 in space that you waste by using 8-bit words. In other words, even if you COULD do it, there would be a performance drop rather than gain.

It MAY be reasonable if there is a 512-bit word machine, since you could then fit 73 7-bit words into a single 512 bit word. But 512-bit word machines aren't that common, and even the AVX512 instruciton set on x86-64 processors is not really 512-byte word, you can't (easily) access every 7th bit as bitfields, you have to shuffle bits in lumps of 8, 16, 32, 64 etc at a time.