gpt4 book ai didi

c++ - ISO/IEC 在 UTF-16 等不同字符集编码中混淆 sizeof(char)

转载 作者:IT老高 更新时间:2023-10-28 12:31:48 38 4
gpt4 key购买 nike

假设一个程序在一个使用 UTF-16 编码字符集的系统上运行。所以根据The C++ Programming Language - 4th,第 150 页:

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

→ 我认为 char 变量的大小为 2 字节。

但根据ISO/IEC 14882:2014:

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

C++ 编程语言 - 第 4 版,第 149 页:

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

→ 固定大小为 1。

问题:以上这些陈述之间是否存在冲突或sizeof(char) = 1 只是一个默认(定义)值,并且将根据每个系统实现定义吗?

最佳答案

C++ 标准(和 C,就此而言)有效地定义了 byte作为 char 的大小类型,不是作为八位数量1。根据 C++11 1.7/1 (我的粗体字):

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.

因此表达式 sizeof(char)总是 1,无论如何。

如果您想查看是否设置了基线 char变量(可能是 unsigned 变体最好)实际上可以保存 16 位值,您要查看的项目是 CHAR_BIT来自 <climits> .这包含 char 中的位数变量。


1 许多标准,尤其是与通信协议(protocol)相关的标准,使用更准确的术语 octet一个八位的值。

关于c++ - ISO/IEC 在 UTF-16 等不同字符集编码中混淆 sizeof(char),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29338126/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com