C++ 指针的大小是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6751749/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:37:01  来源:igfitidea点击:

What is the size of a pointer?

c++pointerssizeof

提问by MGZero

Is the size of a pointer the same as the size as the type its pointing to, or do pointers always have a fixed size? For example...

指针的大小是否与其指向的类型的大小相同,还是指针始终具有固定大小?例如...

int x = 10;
int * xPtr = &x;
char y = 'a';
char * yPtr = &y;

std::cout << sizeof(x) << "\n";
std::cout << sizeof(xPtr) << "\n";
std::cout << sizeof(y) << "\n";
std::cout << sizeof(yPtr) << "\n";

What would the output of this be? Would sizeof(xPtr)return 4 and sizeof(yPtr)return 1, or would the 2 pointers actually return the same size? The reason I ask this is because the pointers are storing a memory address and not the values of their respective stored addresses.

这将是什么输出?会sizeof(xPtr)返回 4 并sizeof(yPtr)返回 1,还是 2 个指针实际上会返回相同的大小?我问这个的原因是因为指针存储的是内存地址,而不是它们各自存储地址的值。

回答by Jens

Function Pointers can have very differentsizes, from 4 to 20 Bytes on an X86 machine, depending on the compiler. So the answer is NO - sizes can vary.

函数指针可以有非常不同的大小,在 X86 机器上从 4 到 20 字节,这取决于编译器。所以答案是否定的 - 尺寸可能会有所不同。

Another example: take an 8051 program, it has three memory ranges and thus has three different pointer sizes, from 8 bit, 16bit, 24bit, depending on where the target is located, even though the target's size is always the same (e.g. char).

另一个例子:以一个 8051 程序为例,它具有三个内存范围,因此具有三种不同的指针大小,从 8 位、16 位、24 位,取决于目标所在的位置,即使目标的大小始终相同(例如字符) .

回答by Nathan Monteleone

Pointers generally have a fixed size, for ex. on a 32-bit executable they're usually 32-bit. There are some exceptions, like on old 16-bit windows when you had to distinguish between 32-bit pointers and 16-bit... It's usually pretty safe to assume they're going to be uniform within a given executable on modern desktop OS's.

指针通常具有固定大小,例如。在 32 位可执行文件上,它们通常是 32 位。有一些例外,比如在旧的 16 位窗口上,当你必须区分 32 位指针和 16 位时......通常可以很安全地假设它们在现代桌面操作系统上的给定可执行文件中是统一的.

Edit: Even so, I would strongly caution against making this assumption in your code. If you're going to write something that absolutely has to have a pointers of a certain size, you'd better check it!

编辑:即便如此,我还是强烈警告不要在你的代码中做出这个假设。如果你要写的东西绝对必须有一定大小的指针,你最好检查一下!

Function pointers are a different story -- see Jens' answerfor more info.

函数指针是另一回事——有关更多信息,请参阅Jens 的回答

回答by peeyush

On 32-bit machine sizeof pointer is 32 bits ( 4 bytes), while on 64 bit machine it's 8 byte. Regardless of what data type they are pointing to, they have fixed size.

在 32 位机器上,指针大小是 32 位(4 字节),而在 64 位机器上是 8 字节。无论它们指向什么数据类型,它们的大小都是固定的。

回答by Jay

To answer your other question. The size of a pointer and the size of what it points to are not related. A good analogy is to consider them like postal addresses. The size of the address of a house has no relationship to the size of the house.

回答你的另一个问题。指针的大小和它指向的东西的大小没有关系。一个很好的比喻是将它们视为邮政地址。房子地址的大小与房子的大小没有关系。

回答by Soumajyoti

Pointers are not always the same size on the same architecture.

指针在同一架构上的大小并不总是相同。

You can read more on the concept of "near", "far" and "huge" pointers, just as an example of a case where pointer sizes differ...

您可以阅读有关“近”、“远”和“大”指针概念的更多信息,仅作为指针大小不同的示例...

http://en.wikipedia.org/wiki/Intel_Memory_Model#Pointer_sizes

http://en.wikipedia.org/wiki/Intel_Memory_Model#Pointer_sizes

回答by Markus Kuhn

They can be different on word-addressable machines (e.g., Cray PVP systems).

它们在可字寻址的机器上可能不同(例如,Cray PVP 系统)。

Most computers today are byte-addressable machines, where each address refers to a byte of memory. There, all data pointers are usually the same size, namely the size of a machine address.

今天的大多数计算机都是可字节寻址的机器,其中每个地址代表一个字节的内存。在那里,所有的数据指针通常都是相同的大小,即一个机器地址的大小。

On word-adressable machines, each machine address refers instead to a word larger than a byte. On these, a (char *) or (void *) pointer to a byte of memory has to contain both a word address plus a byte offset within the addresed word.

在字可寻址机器上,每个机器地址都指向一个大于一个字节的字。在这些上,指向内存字节的 (char *) 或 (void *) 指针必须同时包含字地址和地址字内的字节偏移量。

http://docs.cray.com/books/004-2179-001/html-004-2179-001/rvc5mrwh.html

http://docs.cray.com/books/004-2179-001/html-004-2179-001/rvc5mrwh.html

回答by J T

The size of a pointer is the size required by your system to hold a unique memory address (since a pointer just holds the address it points to)

指针的大小是系统保存唯一内存地址所需的大小(因为指针只保存它指向的地址)