C语言 指针占用多少字节?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20763616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How many bytes do pointers take up?
提问by GovernmentFX
I am little bit confused about pointers and how many bytes they take up. In my textbook it first says that pointers on 16 bit systems take up 2 bytes, 32 bit systems 4 bytes, 64 bit system 8 bytes and so on. Then 10 lines after, it says that pointers take up that many bytes, that are needed to hold the addresses. Here are my questions :
我对指针和它们占用的字节数有点困惑。在我的教科书中,它首先说 16 位系统上的指针占用 2 个字节,32 位系统占用 4 个字节,64 位系统占用 8 个字节,依此类推。然后 10 行之后,它说指针占用了保存地址所需的那么多字节。这是我的问题:
- So does this mean that if we are lets say on 64 bit system, address will need at most 8 bytes?
- If we are on 16 bit system and pointers take 2 bytes, and address needs more the 2 bytes to be placed then what happens?
- 那么这是否意味着如果我们在 64 位系统上说,地址最多需要 8 个字节?
- 如果我们在 16 位系统上,指针占用 2 个字节,而地址需要放置更多的 2 个字节,那么会发生什么?
采纳答案by John Bode
There is no fixed answer; it depends entirely on the architecture, the compiler implementation, and even the type of the pointer itself. Pointers to different types are not guaranteed to have the same size and/or representation.
没有固定的答案;它完全取决于体系结构、编译器实现,甚至指针本身的类型。不保证指向不同类型的指针具有相同的大小和/或表示形式。
For example, assume a word-addressed architecture, where the smallest addressable unit of storage is 16 bits wide (or wider). Each word can hold multiple charvalues; all other types take up a full word or more. On such an architecture, a char *and void *would need some extra bits to offset into the word compared to other pointer types.
例如,假设一个字寻址架构,其中最小的可寻址存储单元是 16 位宽(或更宽)。每个词可以容纳多个char值;所有其他类型占用一个完整的单词或更多。在这样的体系结构上,与其他指针类型相比, achar *和void *需要一些额外的位来偏移到字中。
Note also that a pointer type may be widerthan the number of bits actually required to store an address. The original Macintosh ran on a Motorola 68000 CPU, which had a 32-bit word size, but only 24 bits on the address bus. Pointer types were 32 bits wide, leaving the upper 8 bits unused. Enterprising MacOS programmers took advantage of that to store some data to the uppermost byte of a pointer type, making the most of that precious 128 KB of RAM. Of course, Motorola eventually released a CPU with 32 address lines (the 68020), meaning all that code had to be rewritten.
另请注意,指针类型可能比存储地址实际所需的位数更宽。最初的 Macintosh 运行在 Motorola 68000 CPU 上,它有 32 位字长,但在地址总线上只有 24 位。指针类型为 32 位宽,高 8 位未使用。有进取心的 MacOS 程序员利用这一点将一些数据存储到指针类型的最高字节,从而充分利用了宝贵的 128 KB RAM。当然,摩托罗拉最终发布了具有 32 个地址线的 CPU(68020),这意味着所有代码都必须重写。
On modern, commodity desktop and server hardware (read: x86), it's reasonably safe to assume that all pointer types are the same size as the native word size (32- or 64-bit), and that all pointer types have the same size and representation. Just be aware that this doesn't haveto be true.
在现代商用台式机和服务器硬件(阅读:x86)上,假设所有指针类型的大小与本机字大小(32 位或 64 位)相同,并且所有指针类型具有相同的大小是相当安全的和代表。要知道,这不有是真实的。
回答by NPE
The short answer is that it depends. When we say that a system is 32-bit, this could mean that the native integer is 32 bits wide, that the native address (i.e. pointer size) is 32 bits wide, or both.
简短的回答是,这取决于。当我们说一个系统是 32 位时,这可能意味着本机整数是 32 位宽,本机地址(即指针大小)是 32 位宽,或两者兼而有之。
On top of that, not every architecture uses a flat memory model(for example, see x86 memory segmentation). This further complicates matters.
最重要的是,并非每个架构都使用平面内存模型(例如,请参阅x86 内存分段)。这进一步使问题复杂化。
It is best to treat the size of the pointer as opaque.
最好将指针的大小视为不透明。
C99 provides tools in the form of intptr_tand uintptr_ttypes, which are integers that are guaranteed to be wide enough to hold a pointer.
C99 以intptr_t和uintptr_t类型的形式提供工具,它们是保证足够宽以容纳指针的整数。
回答by Rndp13
The size of the pointer basically depends on the architecture of the system in which it is implemented. For example the size of a pointer in 32 bit is 4 bytes (32 bit ) and 8 bytes(64 bit ) in a 64 bit machines. The bit types in a machine are nothing but memory address, that it can hold. 32 bit machines can hold 2^32and 64 bit machines can hold 2^64address spaces. So a pointer (variable which points to a memory location) should be able to point to any of the memory address (2^32 for 32 bit and 2^64 for 64 bit) that a machines holds.
指针的大小基本上取决于实现它的系统的体系结构。例如,32 位指针的大小在 64 位机器中为 4 字节(32 位)和 8 字节(64 位)。机器中的位类型只不过是它可以保存的内存地址。32 位机器可以2^32容纳2^64地址空间,64 位机器可以容纳地址空间。因此,指针(指向内存位置的变量)应该能够指向2^32 for 32 bit and 2^64 for 64 bit机器拥有的任何内存地址 ( )。
Because of this reason we see the size of a pointer to be 4 bytes in 32 bit machine and 8 bytes in a 64 bit machine.
由于这个原因,我们看到指针的大小在 32 位机器中为 4 个字节,在 64 位机器中为 8 个字节。
Answered in Is the sizeof(some pointer) always equal to four?
回答by Rishu Kumar
Pointers are used to store the address of a variable.The width of the memory address/location depends on the computer architecture.If the computer architecture is 16-bit then it means that it can have 2^16 memory locations.Therefore, for a pointer to be able to store any memory location in this computer it should be 16 bits wide,i.e, 2 bytes(8 bits=1 byte). Similarly for 32-bit and 64-bit architecture we need to have pointers with size 4 bytes(32-bit width) and 8 bytes(64-bit width) respectively.
Also if a system is 16-bit then it cannot have address location of size more than 16 bits.
指针用于存储变量的地址。内存地址/位置的宽度取决于计算机体系结构。如果计算机体系结构是 16 位,则意味着它可以有 2^16 个内存位置。因此,对于一个能够在这台计算机中存储任何内存位置的指针应该是 16 位宽,即 2 字节(8 位 = 1 字节)。同样,对于 32 位和 64 位体系结构,我们需要分别具有大小为 4 字节(32 位宽)和 8 字节(64 位宽)的指针。
此外,如果系统是 16 位,那么它的地址位置不能超过 16 位。
回答by Darren Stone
This will tell you how many bytes it takes to represent a pointer on your system.
这将告诉您表示系统上的指针需要多少字节。
#include <stdio.h>
int main() {
printf("%ld bytes per pointer\n", sizeof(void *));
}
Here's a compiler flag you can play with that's on-topic:
这是一个编译器标志,您可以使用它的主题:
$ gcc -m32 -o prog32 prog.c
$ gcc -m64 -o prog64 prog.c
The first line generates a binary for a 32-bit environment, giving you 4 byte pointers. The second line generates a binary for a 64-bit environment, giving you 8 byte pointers. You can confirm this by running the above program.
第一行为 32 位环境生成一个二进制文件,为您提供 4 个字节的指针。第二行为 64 位环境生成二进制文件,为您提供 8 字节指针。您可以通过运行上述程序来确认这一点。
Assumption, you're on a 64-bit system with GCC. Hope this clarifies things a bit.
假设,您使用的是带有 GCC 的 64 位系统。希望这可以澄清一些事情。

