指针的大小是多少?

发布于 2024-11-24 23:26:30 字数 465 浏览 1 评论 0原文

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

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";

它的输出是什么? sizeof(xPtr) 返回 4,sizeof(yPtr) 返回 1,还是 2 个指针实际上返回相同的大小?

我问这个问题的原因是因为指针存储的是内存地址,而不是它们各自存储地址的值。

Is the size of a pointer the same as the size as the type it's 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

暗藏城府 2024-12-01 23:26:30

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

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

函数指针则是另一回事 - 请参阅Jens 的回答了解更多信息。

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.

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' answer for more info.

何时共饮酒 2024-12-01 23:26:30

函数指针的大小差异很大,在 x86 机器上从 4 到 20 个字节不等,具体取决于编译器。所以答案是否定的 - 尺寸可能会有所不同。

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

Function Pointers can have very different sizes, from 4 to 20 bytes on an x86 machine, depending on the compiler. So the answer is no - sizes can vary.

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

伴我老 2024-12-01 23:26:30

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

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.

孤城病女 2024-12-01 23:26:30

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

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.

一曲琵琶半遮面シ 2024-12-01 23:26:30

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

您可以阅读有关“近”、“远”和“巨大”指针概念的更多信息,就像指针大小不同的情况的示例......

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

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

一影成城 2024-12-01 23:26:30

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

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

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

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

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

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.

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

三五鸿雁 2024-12-01 23:26:30

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

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)

执笔绘流年 2024-12-01 23:26:30

最近遇到的情况并非如此,TI C28x 板可以有 sizeof 指针 == 1,因为这些板的字节是 16 位,而指针大小是 16 位。更令人困惑的是,它们还有 22 位的远指针。我不太确定远指针的大小是多少。

一般来说,DSP 板可能具有奇怪的整数大小。

因此,如果您在奇怪的地方寻找,指针大小在 2020 年仍然可能很奇怪

Recently came upon a case where this was not true, TI C28x boards can have a sizeof pointer == 1, since a byte for those boards is 16-bits, and pointer size is 16 bits. To make matters more confusing, they also have far pointers which are 22-bits. I'm not really sure what sizeof far pointer would be.

In general, DSP boards can have weird integer sizes.

So pointer sizes can still be weird in 2020 if you are looking in weird places

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文