数据类型大小与sizeof(数据类型)之间的差异

时间:2021-04-03 16:34:42

I was learning C++ and come across the following question. I'm just a beginner and I got confused. Isn't sizeof() function supposed to return the size of the datatype? Why could a data object has different size from its sizeof()? I don't understand the explanation of the answer.

我正在学习C ++并遇到以下问题。我只是一个初学者而且我很困惑。是不是sizeof()函数应该返回数据类型的大小?为什么数据对象的大小与sizeof()不同?我不明白答案的解释。

Suppose in a hypothetical machine, the size of char is 32 bits. What would sizeof(char) return?

假设在假设的机器中,char的大小是32位。 sizeof(char)会返回什么?

a) 4

b) 1

c) Implementation dependent

c)依赖于实施

d) Machine dependent

d)机器依赖

Answer:b

Explanation: The standard does NOT require a char to be 8-bits, but does require that sizeof(char) return 1.

说明:标准不要求char为8位,但要求sizeof(char)返回1。

6 个解决方案

#1


12  

The sizeof operator yields the size of a type in bytes, where a byte is defined to be the size of a char. So sizeof(char) is always 1 by definition, regardless of how many bits char has on a given platform.

sizeof运算符以字节为单位生成类型的大小,其中一个字节被定义为char的大小。因此,无论char在给定平台上有多少位,sizeof(char)始终是1。

This applies to both C and C++.

这适用于C和C ++。


From the C11 standard, 6.5.3.4

从C11标准,6.5.3.4

  1. The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand....
  2. sizeof运算符产生其操作数的大小(以字节为单位),该操作数可以是表达式或类型的带括号的名称。大小由操作数的类型决定....

Then,

  1. When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.
  2. 当sizeof应用于具有char,unsigned char或signed char(或其限定版本)类型的操作数时,结果为1。

From the C++11 standard, 5.3.3

从C ++ 11标准,5.3.3

  1. The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.... ... sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1.
  2. sizeof运算符产生其操作数的对象表示中的字节数。操作数是一个表达式,它是一个未评估的操作数(第5章),或带括号的type-id .... ... sizeof(char),sizeof(signed char)和sizeof(unsigned char)是1。

(emphasis mine)

#2


3  

You're just confused with the difference between bytes and octets.

你只是混淆了字节和八位字节之间的差异。

A byte is the size of one character. This yields to the always true sizeof(char) == 1, because sizeof return the size in bytes

一个字节是一个字符的大小。这导致始终为true sizeof(char)== 1,因为sizeof以字节为单位返回大小

While an octet consists out of 8 bits.

八位字节由8位组成。

On almost all modern platforms, the size of a byte is coincidentally the same as of an octet. That's the reason why it's a common error to mix them up, even book authors and professors are doing this.

在几乎所有现代平台上,字节的大小恰好与八位字节相同。这就是为什么将它们混合起来是一个常见的错误,甚至书籍作者和教授都这样做。

#3


3  

Per 5.3.3 [expr.sizeof]

按5.3.3 [expr.sizeof]

The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, to an enumeration type whose underlying type is not fixed before all its enumerators have been declared, to the parenthesized name of such types, or to a glvalue that designates a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1.[...]

sizeof运算符产生其操作数的对象表示中的字节数。操作数是一个表达式,它是一个未评估的操作数(第5章),或带括号的type-id。 sizeof运算符不应用于具有函数或不完整类型的表达式,也不应用于在声明所有枚举数之前其基础类型未修复的枚举类型,此类型的带括号的名称,或指定为glvalue的glvalue。位字段。 sizeof(char),sizeof(signed char)和sizeof(unsigned char)是1。[...]

emphasis mine

So no matter how many bits a char takes up its size is always 1

因此,无论char占用多少位,其大小始终为1

#4


2  

sizeof(x) is returning the size of x expressed in units of size of char.

sizeof(x)返回x的大小,以char的大小为单位表示。

#5


0  

There are no machines where sizeof(char) is 4. It's always 1 byte. That byte might contain 32 bits, but as far as the C compiler is concerned, it's one byte.

没有sizeof(char)为4的机器。它总是1个字节。该字节可能包含32位,但就C编译器而言,它只是一个字节。

The correct name for "8 bits" is octet. The C Standard uses the word "byte" for an object that is the size of a char. Others may use the word "byte" in different ways, often when they mean "octet", but in C (and C++, or Objective-C) it means "object the size of a char". A char may be more than 8 bits, or more than one octet, but it's always one byte.

“8位”的正确名称是八位字节。 C标准使用单词“byte”作为char的大小的对象。其他人可能会以不同的方式使用“byte”这个词,通常是指“octet”,但在C(和C ++,或Objective-C)中,它意味着“对象是char的大小”。 char可能超过8位,或多于一个八位位组,但它总是一个字节。

#6


-1  

The question should have been -- Suppose in a hypothetical machine, the word size(size of registers) is 32 bits. What would sizeof(char) return?

问题应该是 - 假设在假设的机器中,字大小(寄存器的大小)是32位。 sizeof(char)会返回什么?

And answer will be 1 byte.

答案是1个字节。

In computing, word is a term for the natural unit of data used by a particular processor design. A word is a fixed-sized piece of data handled as a unit by the instruction set or the hardware of the processor. The number of bits in a word (the word size, word width, or word length) is an important characteristic of any specific processor design or computer architecture. -- https://en.wikipedia.org/wiki/Word_%28computer_architecture%29

在计算中,word是特定处理器设计所使用的自然数据单元的术语。字是由指令集或处理器的硬件作为一个单元处理的固定大小的数据。字中的位数(字长,字宽或字长)是任何特定处理器设计或计算机体系结构的重要特征。 - https://en.wikipedia.org/wiki/Word_%28computer_architecture%29

In you case word-size will be 32 bits. Also

在你的情况下,字大小将是32位。也

Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit of memory in many computer architectures. -- https://en.wikipedia.org/wiki/Byte

从历史上看,字节是用于在计算机中编码单个文本字符的位数,因此它是许多计算机体系结构中最小的可寻址存储单元。 - https://en.wikipedia.org/wiki/Byte

1 byte is smallest addressable unit of memory, it cat be 8 bit , 9 bits or 16 bits anything that hardware spec chooses.

1字节是存储器的最小可寻址单元,它是8位,9位或16位硬件规格选择的任何东西。

As far as sizeof is concerned it first determines the type of argument, eventually computes the size in bytes. So, following two C++ statements will produce same result.

就sizeof而言,它首先确定参数的类型,最终以字节为单位计算大小。因此,遵循两个C ++语句将产生相同的结果。

  int n;
  std::cout<<sizeof(int);
  std::cout<<sizeof(n);

#1


12  

The sizeof operator yields the size of a type in bytes, where a byte is defined to be the size of a char. So sizeof(char) is always 1 by definition, regardless of how many bits char has on a given platform.

sizeof运算符以字节为单位生成类型的大小,其中一个字节被定义为char的大小。因此,无论char在给定平台上有多少位,sizeof(char)始终是1。

This applies to both C and C++.

这适用于C和C ++。


From the C11 standard, 6.5.3.4

从C11标准,6.5.3.4

  1. The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand....
  2. sizeof运算符产生其操作数的大小(以字节为单位),该操作数可以是表达式或类型的带括号的名称。大小由操作数的类型决定....

Then,

  1. When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.
  2. 当sizeof应用于具有char,unsigned char或signed char(或其限定版本)类型的操作数时,结果为1。

From the C++11 standard, 5.3.3

从C ++ 11标准,5.3.3

  1. The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id.... ... sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1.
  2. sizeof运算符产生其操作数的对象表示中的字节数。操作数是一个表达式,它是一个未评估的操作数(第5章),或带括号的type-id .... ... sizeof(char),sizeof(signed char)和sizeof(unsigned char)是1。

(emphasis mine)

#2


3  

You're just confused with the difference between bytes and octets.

你只是混淆了字节和八位字节之间的差异。

A byte is the size of one character. This yields to the always true sizeof(char) == 1, because sizeof return the size in bytes

一个字节是一个字符的大小。这导致始终为true sizeof(char)== 1,因为sizeof以字节为单位返回大小

While an octet consists out of 8 bits.

八位字节由8位组成。

On almost all modern platforms, the size of a byte is coincidentally the same as of an octet. That's the reason why it's a common error to mix them up, even book authors and professors are doing this.

在几乎所有现代平台上,字节的大小恰好与八位字节相同。这就是为什么将它们混合起来是一个常见的错误,甚至书籍作者和教授都这样做。

#3


3  

Per 5.3.3 [expr.sizeof]

按5.3.3 [expr.sizeof]

The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, to an enumeration type whose underlying type is not fixed before all its enumerators have been declared, to the parenthesized name of such types, or to a glvalue that designates a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1.[...]

sizeof运算符产生其操作数的对象表示中的字节数。操作数是一个表达式,它是一个未评估的操作数(第5章),或带括号的type-id。 sizeof运算符不应用于具有函数或不完整类型的表达式,也不应用于在声明所有枚举数之前其基础类型未修复的枚举类型,此类型的带括号的名称,或指定为glvalue的glvalue。位字段。 sizeof(char),sizeof(signed char)和sizeof(unsigned char)是1。[...]

emphasis mine

So no matter how many bits a char takes up its size is always 1

因此,无论char占用多少位,其大小始终为1

#4


2  

sizeof(x) is returning the size of x expressed in units of size of char.

sizeof(x)返回x的大小,以char的大小为单位表示。

#5


0  

There are no machines where sizeof(char) is 4. It's always 1 byte. That byte might contain 32 bits, but as far as the C compiler is concerned, it's one byte.

没有sizeof(char)为4的机器。它总是1个字节。该字节可能包含32位,但就C编译器而言,它只是一个字节。

The correct name for "8 bits" is octet. The C Standard uses the word "byte" for an object that is the size of a char. Others may use the word "byte" in different ways, often when they mean "octet", but in C (and C++, or Objective-C) it means "object the size of a char". A char may be more than 8 bits, or more than one octet, but it's always one byte.

“8位”的正确名称是八位字节。 C标准使用单词“byte”作为char的大小的对象。其他人可能会以不同的方式使用“byte”这个词,通常是指“octet”,但在C(和C ++,或Objective-C)中,它意味着“对象是char的大小”。 char可能超过8位,或多于一个八位位组,但它总是一个字节。

#6


-1  

The question should have been -- Suppose in a hypothetical machine, the word size(size of registers) is 32 bits. What would sizeof(char) return?

问题应该是 - 假设在假设的机器中,字大小(寄存器的大小)是32位。 sizeof(char)会返回什么?

And answer will be 1 byte.

答案是1个字节。

In computing, word is a term for the natural unit of data used by a particular processor design. A word is a fixed-sized piece of data handled as a unit by the instruction set or the hardware of the processor. The number of bits in a word (the word size, word width, or word length) is an important characteristic of any specific processor design or computer architecture. -- https://en.wikipedia.org/wiki/Word_%28computer_architecture%29

在计算中,word是特定处理器设计所使用的自然数据单元的术语。字是由指令集或处理器的硬件作为一个单元处理的固定大小的数据。字中的位数(字长,字宽或字长)是任何特定处理器设计或计算机体系结构的重要特征。 - https://en.wikipedia.org/wiki/Word_%28computer_architecture%29

In you case word-size will be 32 bits. Also

在你的情况下,字大小将是32位。也

Historically, the byte was the number of bits used to encode a single character of text in a computer and for this reason it is the smallest addressable unit of memory in many computer architectures. -- https://en.wikipedia.org/wiki/Byte

从历史上看,字节是用于在计算机中编码单个文本字符的位数,因此它是许多计算机体系结构中最小的可寻址存储单元。 - https://en.wikipedia.org/wiki/Byte

1 byte is smallest addressable unit of memory, it cat be 8 bit , 9 bits or 16 bits anything that hardware spec chooses.

1字节是存储器的最小可寻址单元,它是8位,9位或16位硬件规格选择的任何东西。

As far as sizeof is concerned it first determines the type of argument, eventually computes the size in bytes. So, following two C++ statements will produce same result.

就sizeof而言,它首先确定参数的类型,最终以字节为单位计算大小。因此,遵循两个C ++语句将产生相同的结果。

  int n;
  std::cout<<sizeof(int);
  std::cout<<sizeof(n);