是什么决定了原始数据类型的大小?

时间:2022-02-02 16:27:31

For one of my C++ programming assignments, I have to consider the size of an integer variable while designing the program. I read around the internet, and most places say "The size of an integer is dependent on the platform." I'm unclear about what this means, so I'm asking here.

对于我的一个C ++编程分配,我必须在设计程序时考虑整数变量的大小。我在互联网上阅读,大多数地方都说“整数的大小取决于平台。”我不清楚这意味着什么,所以我在这里问。

  1. What determines the size of a primitive data type?

    是什么决定了原始数据类型的大小?

    • processor architecture (may be instruction set size).
    • 处理器架构(可以是指令集大小)。

    • Operation system
    • Compiler
    • Combination of above.
    • 以上的组合。

  2. What is the reason to choose an integer to have a size of 2 bytes in some systems, and 4 bytes in others? Is there any reason it cannot proceed with 2 byte anymore?

    选择一个整数在某些系统中的大小为2个字节,在其他系统中为4个字节的原因是什么?有什么理由不能继续2字节吗?

4 个解决方案

#1


4  

What determines the size of a primitive data type?

是什么决定了原始数据类型的大小?

It depends on Compiler. Compiler in turns usually depends on the architecture, processor, development environment etc because it takes them into account. So you may say it's a combination of all.

这取决于编译器。编译器通常依赖于体系结构,处理器,开发环境等,因为它将它们考虑在内。所以你可能会说它是所有的组合。

What is the reason to choose an integer to have a size of 2 bytes in some systems, and 4 bytes in others? Is there any reason it cannot proceed with 2 byte anymore?

选择一个整数在某些系统中的大小为2个字节,在其他系统中为4个字节的原因是什么?有什么理由不能继续2字节吗?

The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold. You can infer minimum size in bits from the required range. You can infer minimum size in bytes from that and the value of the CHAR_BIT macro that defines the number of bits in a byte (in all but the most obscure platforms it's 8, and it can't be less than 8).

C ++标准没有以字节为单位指定整数类型的大小,但它指定了它们必须能够容纳的最小范围。您可以从所需范围推断出最小位数。您可以从中推断出最小大小(以字节为单位)以及CHAR_BIT宏的值,该值定义了一个字节中的位数(除了最不起眼的平台之外,它只有8个,并且不能小于8)。

Check out here for more info.

点击这里了解更多信息。

#2


6  

whats the platform they means here.

他们在这里意味着什么平台。

Usually, it means the combination of operating system, compiler, and some special options of compiler.

通常,它表示操作系统,编译器和编译器的一些特殊选项的组合。

what cause to decide the primitive data type sizes.

是什么原因决定原始数据类型的大小。

That would be 'Combination of above.'

那就是“上面的组合”。


By the way, this is called 'memory model' or 'data model' (not sure which one is the correct term), you could learn more about it from http://en.wikipedia.org/wiki/64-bit

顺便说一句,这被称为“记忆模型”或“数据模型”(不确定哪一个是正确的术语),您可以从http://en.wikipedia.org/wiki/64-bit了解更多信息。

#3


1  

It seems your first question has been answered already so I'll take a stab at the second one:

看来你的第一个问题已经回答了,所以我会在第二个问题上做一个问题:

What is the reason to choose an integer to have a size of 2 bytes in some systems, and 4 bytes in others? Is there any reason it cannot proceed with 2 bytes anymore?

选择一个整数在某些系统中的大小为2个字节,在其他系统中为4个字节的原因是什么?有什么理由不再继续使用2个字节吗?

This boils down to the opinions of the designers of an architecture (or compiler, etc.). Very often, memory addresses are represented as unsigned integer data types. So you get this interesting history of integer sizes that reflect the size of an address in an operating system. That is, it's really no coincidence that:

这归结为架构(或编译器等)的设计者的意见。通常,内存地址表示为无符号整数数据类型。因此,您将获得有趣的整数大小历史记录,它反映了操作系统中地址的大小。也就是说,这并非巧合:

cout << "Unsigned short: " << sizeof(unsigned short) << endl;
cout << "Unsigned int: " << sizeof(unsigned int) << endl;
cout << "Unsigned long: " << sizeof(unsigned long) << endl;

Produces the following output on most systems:

在大多数系统上生成以下输出:

Unsigned short: 2
Unsigned int: 4
Unsigned long: 8

Because a byte is 8 bits, you get the following sizes:

因为一个字节是8位,所以你得到以下大小:

  • Unsigned short: 16-bit
  • 无符号短路:16位

  • Unsigned int: 32-bit
  • 无符号整数:32位

  • Unsigned long: 64-bit
  • 无符号长:64位

You can make a good bet that if we get to 128-bit operating systems, there will be a data type (maybe unsigned long long?) that consists of 16 bytes.

您可以打赌,如果我们进入128位操作系统,将会有一个由16个字节组成的数据类型(可能是无符号长整数?)。

So it's history in a sense, but Why would an architect decide on these sizes to begin with? Well a few reasons:

从某种意义上说它是历史,但为什么建筑师会首先决定这些尺寸?原因如下:

  1. "It's big enough" Computer Scientists are fond of doing only as much as they need to, so an integer data type any bigger would likely be wasteful in most use cases because people simply don't need to count that high with a primitive data type.
  2. “它足够大”计算机科学家喜欢只做他们需要的东西,所以在大多数用例中,任何更大的整数数据类型都可能是浪费,因为人们根本不需要用原始数据类型计算那么高。

  3. "It's as big as we can get" You'd like an integer to fit entirely within a single register, so the size is usually limited by hardware (registers being only one example).
  4. “它就像我们能得到的一样大”你想要一个整数完全适合单个寄存器,所以大小通常受硬件限制(寄存器只是一个例子)。

I'm sure there are other reasons that smarter, more experienced * members can give :-)

我确信有更聪明,更有经验的*成员可以提供的其他原因:-)

#4


1  

In C the size of int is 4 bytes in gcc(GNU collection of compilers) and 2 bytes in borland and turbo c compiler. The last two compiler is specific for windows and and gcc compiler is the compiler for Linux OS. The size of primitive data types is based on compiler and these compiler is basically specific for specific OS. I personally preferred gcc compiler over other compilers because it support lots of features according to the definition of C language. One of the example is null pointer. If you declare a null pointer in gcc and try to access this in gcc it gives you error but turbo c allows to use it no error.

在C中,int的大小是gcc(GNU编译器集合)中的4个字节,以及borland和turbo c编译器中的2个字节。最后两个编译器特定于Windows,而gcc编译器是Linux OS的编译器。原始数据类型的大小基于编译器,这些编译器基本上特定于特定的OS。我个人更喜欢gcc编译器而不是其他编译器,因为它根据C语言的定义支持许多功能。其中一个例子是空指针。如果你在gcc中声明一个空指针并尝试在gcc中访问它,它会给你错误但turbo c允许使用它没有错误。

#1


4  

What determines the size of a primitive data type?

是什么决定了原始数据类型的大小?

It depends on Compiler. Compiler in turns usually depends on the architecture, processor, development environment etc because it takes them into account. So you may say it's a combination of all.

这取决于编译器。编译器通常依赖于体系结构,处理器,开发环境等,因为它将它们考虑在内。所以你可能会说它是所有的组合。

What is the reason to choose an integer to have a size of 2 bytes in some systems, and 4 bytes in others? Is there any reason it cannot proceed with 2 byte anymore?

选择一个整数在某些系统中的大小为2个字节,在其他系统中为4个字节的原因是什么?有什么理由不能继续2字节吗?

The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold. You can infer minimum size in bits from the required range. You can infer minimum size in bytes from that and the value of the CHAR_BIT macro that defines the number of bits in a byte (in all but the most obscure platforms it's 8, and it can't be less than 8).

C ++标准没有以字节为单位指定整数类型的大小,但它指定了它们必须能够容纳的最小范围。您可以从所需范围推断出最小位数。您可以从中推断出最小大小(以字节为单位)以及CHAR_BIT宏的值,该值定义了一个字节中的位数(除了最不起眼的平台之外,它只有8个,并且不能小于8)。

Check out here for more info.

点击这里了解更多信息。

#2


6  

whats the platform they means here.

他们在这里意味着什么平台。

Usually, it means the combination of operating system, compiler, and some special options of compiler.

通常,它表示操作系统,编译器和编译器的一些特殊选项的组合。

what cause to decide the primitive data type sizes.

是什么原因决定原始数据类型的大小。

That would be 'Combination of above.'

那就是“上面的组合”。


By the way, this is called 'memory model' or 'data model' (not sure which one is the correct term), you could learn more about it from http://en.wikipedia.org/wiki/64-bit

顺便说一句,这被称为“记忆模型”或“数据模型”(不确定哪一个是正确的术语),您可以从http://en.wikipedia.org/wiki/64-bit了解更多信息。

#3


1  

It seems your first question has been answered already so I'll take a stab at the second one:

看来你的第一个问题已经回答了,所以我会在第二个问题上做一个问题:

What is the reason to choose an integer to have a size of 2 bytes in some systems, and 4 bytes in others? Is there any reason it cannot proceed with 2 bytes anymore?

选择一个整数在某些系统中的大小为2个字节,在其他系统中为4个字节的原因是什么?有什么理由不再继续使用2个字节吗?

This boils down to the opinions of the designers of an architecture (or compiler, etc.). Very often, memory addresses are represented as unsigned integer data types. So you get this interesting history of integer sizes that reflect the size of an address in an operating system. That is, it's really no coincidence that:

这归结为架构(或编译器等)的设计者的意见。通常,内存地址表示为无符号整数数据类型。因此,您将获得有趣的整数大小历史记录,它反映了操作系统中地址的大小。也就是说,这并非巧合:

cout << "Unsigned short: " << sizeof(unsigned short) << endl;
cout << "Unsigned int: " << sizeof(unsigned int) << endl;
cout << "Unsigned long: " << sizeof(unsigned long) << endl;

Produces the following output on most systems:

在大多数系统上生成以下输出:

Unsigned short: 2
Unsigned int: 4
Unsigned long: 8

Because a byte is 8 bits, you get the following sizes:

因为一个字节是8位,所以你得到以下大小:

  • Unsigned short: 16-bit
  • 无符号短路:16位

  • Unsigned int: 32-bit
  • 无符号整数:32位

  • Unsigned long: 64-bit
  • 无符号长:64位

You can make a good bet that if we get to 128-bit operating systems, there will be a data type (maybe unsigned long long?) that consists of 16 bytes.

您可以打赌,如果我们进入128位操作系统,将会有一个由16个字节组成的数据类型(可能是无符号长整数?)。

So it's history in a sense, but Why would an architect decide on these sizes to begin with? Well a few reasons:

从某种意义上说它是历史,但为什么建筑师会首先决定这些尺寸?原因如下:

  1. "It's big enough" Computer Scientists are fond of doing only as much as they need to, so an integer data type any bigger would likely be wasteful in most use cases because people simply don't need to count that high with a primitive data type.
  2. “它足够大”计算机科学家喜欢只做他们需要的东西,所以在大多数用例中,任何更大的整数数据类型都可能是浪费,因为人们根本不需要用原始数据类型计算那么高。

  3. "It's as big as we can get" You'd like an integer to fit entirely within a single register, so the size is usually limited by hardware (registers being only one example).
  4. “它就像我们能得到的一样大”你想要一个整数完全适合单个寄存器,所以大小通常受硬件限制(寄存器只是一个例子)。

I'm sure there are other reasons that smarter, more experienced * members can give :-)

我确信有更聪明,更有经验的*成员可以提供的其他原因:-)

#4


1  

In C the size of int is 4 bytes in gcc(GNU collection of compilers) and 2 bytes in borland and turbo c compiler. The last two compiler is specific for windows and and gcc compiler is the compiler for Linux OS. The size of primitive data types is based on compiler and these compiler is basically specific for specific OS. I personally preferred gcc compiler over other compilers because it support lots of features according to the definition of C language. One of the example is null pointer. If you declare a null pointer in gcc and try to access this in gcc it gives you error but turbo c allows to use it no error.

在C中,int的大小是gcc(GNU编译器集合)中的4个字节,以及borland和turbo c编译器中的2个字节。最后两个编译器特定于Windows,而gcc编译器是Linux OS的编译器。原始数据类型的大小基于编译器,这些编译器基本上特定于特定的OS。我个人更喜欢gcc编译器而不是其他编译器,因为它根据C语言的定义支持许多功能。其中一个例子是空指针。如果你在gcc中声明一个空指针并尝试在gcc中访问它,它会给你错误但turbo c允许使用它没有错误。