且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么任何数据类型的大小都取决于机器?

更新时间:2023-02-13 16:08:41

对于初学者来说,机器的架构,比如 16 位或 64 位,将决定地址总线的宽度,并有效地决定您可以访问多少内存无需借助虚拟内存之类的技巧.

For starters, the machine's architecture, say 16 bit or 64 bits, will determine how wide the address bus is, and that effectively determines how much memory you can access without resorting to tricks like virtual memory.

通常,CPU 内部的硬件寄存器与架构的其余部分具有相同的位宽;因此,在 32 位 CPU 上,以 32 位块加载和存储数据通常是最有效的.在某些环境中,甚至有必要将所有数据对齐在 32 位边界上,即无法从字节地址不能被 4 整除的内存地址中获取数据.

Usually, the hardware registers inside the CPU will have the same bit width as most of the rest of the architecture; so on a 32 bit CPU it will usually be most efficient to load and store data in 32 bit chunks. In some environments, it's even necessary that all your data be aligned on 32 bit boundaries, i.e. you can't grab data from a memory address whose byte address is not divisible by 4.

在软件中绕过所有这些限制是可能的,但对于最高效的程序,您会希望编译器的数据类型与系统硬件的数据类型密切相关.

It's possible to circumvent all these restrictions in software, but for the most efficient programs, you will want your compiler's data types to be closely related to the system hardware's.