指针可以有不同的大小吗? [英] Can pointers be of different sizes?
问题描述
这个答案有一个有趣的声明 - 在 int *
小于 char *
。 (让我们排除指向函数的指针)
This answer comes with an interesting statement - "on machines where int*
is smaller than a char*
". (let's exclude pointers to functions)
有可能指向不同类型的指针有不同的大小吗?为什么这很有用?
Is it possible for pointers to different types to have different sizes? Why would this be useful?
推荐答案
是的,这是完全可能的。在一些机器上,指向字节的指针包含两个值:指向包含字节的存储器字的WORD地址的指针,以及给出字中字节位置的字节索引。例如。在32位机器上,字节索引为0..3。
Yes, it's entirely possible. On some machines, a pointer to a byte contains two values: A pointer to the WORD address of the memory word containing the byte, and a "byte index" that gives the position of the byte within the word. E.g. on a 32-bit machine, the "byte index" is 0..3.
这将需要比int *更多的存储空间,这只是一个指向相关单词的指针。
This would require more storage space than a "int *", which is just a pointer to the relevant word.
这篇关于指针可以有不同的大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!