37
votes

Assuming that a program is running on a system with UTF-16 encoding character set. So according to The C++ Programming Language - 4th, page 150:

A char can hold a character of the machine’s character set.

→ I think that a char variable will have the size is 2-bytes.

But according to ISO/IEC 14882:2014:

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1".

or The C++ Programming Language - 4th, page 149:

"[...], so by definition the size of a char is 1"

→ It is fixed with size is 1.

Question: Is there a conflict between these statements above or is the sizeof(char) = 1 just a default (definition) value and will be implementation-defined depends on each system?

5
what do you mean with "a system with UTF-16 encoding" basically in c++ you are resposible for your own encoding. and utf-16 strings are stored in wchar arrays/strings. - Arne
@Arne: It is perfectly conceivable that a system could use UTF-16 as its default basic encoding. - Dolda2000
@Arne: Of course not, but that's besides the point. The point of the question is that "The C++ Programming Language" defines char in terms of "the machine's character set". - Dolda2000
Historically "a character" was a common use for "a byte", and at the lowest levels they were essentially the same thing. Both the move that meant that almost (but still not quite) all computers had bytes that are octets (8-bits) and the move that meant we used more than one byte per character so we can represent real text well came later. The legacy of this is that while some later languages have a char type suitable for use with characters separate to a byte type, in C and C++ char is a name for bytes. - Jon Hanna
@Arne: wcchar and wstring are absolutely not your friend when you want UTF-16, in contrast to an unspecified wide encoding, which is most likely actually the fixed-size UTF-32. - Deduplicator

5 Answers

35
votes

The C++ standard (and C, for that matter) effectively define byte as the size of a char type, not as an eight-bit quantity1. As per C++11 1.7/1 (my bold):

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 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.

Hence the expression sizeof(char) is always 1, no matter what.

If you want to see whether you baseline char variable (probably the unsigned variant would be best) can actually hold a 16-bit value, the item you want to look at is CHAR_BIT from <climits>. This holds the number of bits in a char variable.


1 Many standards, especially ones related to communications protocols, use the more exact term octet for an eight-bit value.

28
votes

Yes, there are a number of serious conflicts and problems with the C++ conflation of rôles for char, but also the question conflates a few things. So a simple direct answer would be like answering “yes”, “no” or “don’t know” to the question “have you stopped beating your wife?”. The only direct answer is the buddhist “mu”, unasking the question.

So let's therefore start with a look at the facts.


Facts about the char type.

The number of bits per char is given by the implementation defined CHAR_BIT from the <limits.h> header. This number is guaranteed to be 8 or larger. With C++03 and earlier that guarantee came from the specification of that symbol in the C89 standard, which the C++ standard noted (in a non-normative section, but still) as “incorporated”. With C++11 and later the C++ standard explicitly, on its own, gives the ≥8 guarantee. On most platforms CHAR_BIT is 8, but on some probably still extant Texas Instruments digital signal processors it’s 16, and other values have been used.

Regardless of the value of CHAR_BIT the sizeof(char) is by definition 1, i.e. it's not implementation defined:

C++11 §5.3.3/1

sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1.

That is, char and its variants is the fundamental unit of addressing of memory, which is the primary meaning of byte, both in common speech and formally in C++:

C++11 §1.7/1

The fundamental storage unit in the C ++ memory model is the byte.

This means that on the aforementioned TI DSPs, there is no C++ way of obtaining pointers to individual octets (8-bit parts). And that in turn means that code that needs to deal with endianness, or in other ways needs to treat char values as sequences of octets, in particular for network communications, needs to do things with char values that is not meaningful on a system where CHAR_BIT is 8. It also means that ordinary C++ narrow string literals, if they adhere to the standard, and if the platform's standard software uses an 8-bit character encoding, will waste memory.

The waste aspect was (or is) directly addressed in the Pascal language, which differentiates between packed strings (multiple octets per byte) and unpacked strings (one octet per byte), where the former is used for passive text storage, and the latter is used for efficient processing.

This illustrates the basic conflation of three aspects in the single C++ type char:

  • unit of memory addressing, a.k.a. byte,

  • smallest basic type (it would be nice with an octet type), and

  • character encoding value unit.

And yes, this is a conflict.


Facts about UTF-16 encoding.

Unicode is a large set of 21-bit code points, most of which constitute characters on their own, but some of which are combined with others to form characters. E.g. a character with accent like “é” can be formed by combining code points for “e” and “´”-as-accent. And since that’s a general mechanism it means that a Unicode character can be an arbitrary number of code points, although it’s usually just 1.

UTF-16 encoding was originally a compatibility scheme for code based on original Unicode’s 16 bits per code point, when Unicode was extended to 21 bits per code point. The basic scheme is that code points in the defined ranges of original Unicode are represented as themselves, while each new Unicode code point is represented as a surrogate pair of 16-bit values. A small range of original Unicode is used for surrogate pair values.

At the time, examples of software based on 16 bits per code point included 32-bit Windows and the Java language.

On a system with 8-bit byte UTF-16 is an example of a wide text encoding, i.e. with an encoding unit wider than the basic addressable unit. Byte oriented text encodings are then known as narrow text. On such a system C++ char fits the latter, but not the former.

In C++03 the only built in type suitable for the wide text encoding unit was wchar_t.

However, the C++ standard effectively requires wchar_t to be suitable for a code-point, which for modern 21-bits-per-code-point Unicode means that it needs to be 32 bits. Thus there is no C++03 dedicated type that fits the requirements of UTF-16 encoding values, 16 bits per value. Due to historical reasons the most prevalent system based on UTF-16 as wide text encoding, namely Microsoft Windows, defines wchar_t as 16 bits, which after the extension of Unicode has been in flagrant contradiction with the standard, but then, the standard is impractical regarding this issue. Some platforms define wchar_t as 32 bits.

C++11 introduced new types char16_t and char32_t, where the former is (designed to be) suitable for UTF-16 encoding values.


About the question.

Regarding the question’s stated assumption of

” a system with UTF-16 encoding character set

this can mean one of two things:

  • a system with UTF-16 as the standard narrow encoding, or
  • a system with UTF-16 as the standard wide encoding.

With UTF-16 as the standard narrow encoding CHAR_BIT ≥ 16, and (by definition) sizeof(char) = 1. I do not know of any system, i.e. it appears to be hypothetical. Yet it appears to be the meaning tacitly assumed in current other answers.

With UTF-16 as the standard wide encoding, as in Windows, the situation is more complex, because the C++ standard is not up to the task. But, to use Windows as an example, one practical possibility is that sizeof(wchar_t)= 2. And one should just note that the standard is conflict with existing practice and practical considerations for this issue, when the ideal is that standards instead standardize the existing practice, where there is such.

Now finally we’re in a position to deal with the question,

Is there a conflict between these statements above or is the sizeof(char) = 1 just a default (definition) value and will be implementation-defined depends on each system?

This is a false dichotomy. The two possibilities are not opposites. We have

  • There is indeed a conflict between char as character encoding unit and as a memory addressing unit (byte). As noted, the Pascal language has the keyword packed to deal with one aspect of that conflict, namely storage versus processing requirements. And there is a further conflict between the formal requirements on wchar_t, and its use for UTF-16 encoding in the most widely used system that employs UTF-16 encoding, namely Windows.

  • sizeof(char) = 1 by definition: it's not system-dependent.

  • CHAR_BIT is implementation defined, and is guaranteed ≥ 8.

9
votes

No, there's no conflict. These two statements refer to different definitions of byte.

UTF-16 implies that byte is the same thing as octet - a group of 8 bits.

In C++ language byte is the same thing as char. There's no limitation on how many bits a C++-byte can contain. The number of bits in C++-byte is defined by CHAR_BIT macro constant.

If your C++ implementation decides to use 16 bits to represent each character, then CHAR_BIT will be 16 and each C++-byte will occupy two UTF-16-bytes. sizeof(char) will still be 1 and sizes of all objects will be measured in terms of 16-bit bytes.

7
votes

A char is defined as being 1 byte. A byte is the smallest addressable unit. This is 8 bits on common systems, but on some systems it is 16 bits, or 32 bits, or anything else (but must be at least 8 for C++).

It is somewhat confusing because in popular jargon byte is used for what is technically known as an octet (8 bits).

So, your second and third quotes are correct. The first quote is, strictly speaking, not correct.

As defined by [intro.memory]/1 in the C++ Standard, char only needs to be able to hold the basic execution character set which is approximately 100 characters (all of which appear in the 0 - 127 range of ASCII), and the octets that make up UTF-8 encoding. Perhaps that is what the author meant by machine character set.


On a system where the hardware is octet addressable but the character set is Unicode, it is likely that char will remain 8-bit. However there are types char16_t and char32_t (added in C++11) which are designed to be used in your code instead of char for systems that have 16-bit or 32-bit character sets.

So, if the system goes with char16_t then you would use std::basic_string<char16_t> instead of std::string, and so on.

Exactly how UTF-16 should be handled will depend on the detail of the implementation chosen by the system. Unicode is a 21-bit character set and UTF-16 is a multibyte encoding of it; so the system could go the Windows-like route and use std::basic_string<char16_t> with UTF-16 encoding for strings; or it could go for std::basic_string<char32_t> with raw Unicode code points as the characters.

Alf's post goes into more detail on some of the issues that can arise.

5
votes

Without quoting standard it is easily to give simply answer because:

Definition of byte is not 8 bits. Byte is any size but smallest addressable unit of memory. Most commonly it is 8 bits, but there is no reason to don't have 16 bits byte.

C++ standard gives more restrictions because it must be at least 8 bits.

So there is no problem to sizeof(char) be always 1, no matter what. Sometimes it will be stand as 8 bits, sometimes 16 bits and so on.