是否有任何平台指向不同类型的指针具有不同的大小? [英] Are there any platforms where pointers to different types have different sizes?

查看:25
本文介绍了是否有任何平台指向不同类型的指针具有不同的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C 标准允许指向不同类型的指针具有不同的大小,例如sizeof(char*) != sizeof(int*) 是允许的.然而,它确实要求如果一个指针被转换为 void* 然后转换回它的原始类型,它必须与它的原始值进行比较.因此,从逻辑上讲,sizeof(void*) >= sizeof(T*) 对于所有类型T,对吗?

The C standard allows pointers to different types to have different sizes, e.g. sizeof(char*) != sizeof(int*) is permitted. It does, however, require that if a pointer is converted to a void* and then converted back to its original type, it must compare as equal to its original value. Therefore, it follows logically that sizeof(void*) >= sizeof(T*) for all types T, correct?

在当今使用的大多数常见平台(x86、PPC、ARM 和 64 位变体等)上,所有指针的大小都等于本机寄存器大小(4 或 8 字节),无论指向的是类型.是否有任何深奥或嵌入式平台,其中指向不同类型的指针可能具有不同的大小?我特别询问数据指针,尽管我也很想知道是否存在函数指针具有异常大小的平台.

On most common platforms in use today (x86, PPC, ARM, and 64-bit variants, etc.), the size of all pointers equals the native register size (4 or 8 bytes), regardless of the pointed-to type. Are there any esoteric or embedded platforms where pointers to different types might have different sizes? I'm specifically asking about data pointers, although I'd also be interested to know if there are platforms where function pointers have unusual sizes.

我绝对不是询问 C++ 的成员指针和成员函数指针.这些在通用平台上具有不同寻常的大小,甚至在一个平台内可能会有所不同,具体取决于指向类的属性(非多态、单继承、多继承、虚拟继承或不完整类型).

I'm definitely not asking about C++'s pointer-to-members and pointer-to-member-functions. Those take on unusual sizes on common platforms, and can even vary within one platform, depending on the properties of the pointer-to class (non-polymorphic, single inheritance, multiple inheritance, virtual inheritance, or incomplete type).

推荐答案

来自 C FAQ 的答案:

Prime 50 系列使用段 07777,空指针的偏移量为 0,至少对于 PL/I.后来的模型在 C 中使用段 0、偏移量 0 来表示空指针,因此需要新的指令,例如 TCNP(测试 C 空指针),这显然是对所有现有编写不当的 C 代码做出错误假设的一种替代.较旧的字寻址 Prime 机器也因需要比字指针(int *)更大的字节指针(char *)而臭名昭著.

The Prime 50 series used segment 07777, offset 0 for the null pointer, at least for PL/I. Later models used segment 0, offset 0 for null pointers in C, necessitating new instructions such as TCNP (Test C Null Pointer), evidently as a sop to all the extant poorly-written C code which made incorrect assumptions. Older, word-addressed Prime machines were also notorious for requiring larger byte pointers (char *'s) than word pointers (int *'s).

Data General 的 Eclipse MV 系列具有三种体系结构支持的指针格式(字、字节和位指针),其中两种被 C 编译器使用:用于 char * 和 void * 的字节指针,以及用于其他所有内容的字指针.由于在 32 位 MV 线从 16 位 Nova 线演变过程中的历史原因,字指针和字节指针在字的不同位置具有偏移、间接和环保护位.将不匹配的指针格式传递给函数会导致保护错误.最终,MVC 编译器添加了许多兼容性选项来尝试处理存在指针类型不匹配错误的代码.

The Eclipse MV series from Data General has three architecturally supported pointer formats (word, byte, and bit pointers), two of which are used by C compilers: byte pointers for char * and void *, and word pointers for everything else. For historical reasons during the evolution of the 32-bit MV line from the 16-bit Nova line, word pointers and byte pointers had the offset, indirection, and ring protection bits in different places in the word. Passing a mismatched pointer format to a function resulted in protection faults. Eventually, the MV C compiler added many compatibility options to try to deal with code that had pointer type mismatch errors.

某些 Honeywell-Bull 大型机将位模式 06000 用于(内部)空指针.

Some Honeywell-Bull mainframes use the bit pattern 06000 for (internal) null pointers.

CDC Cyber​​ 180 系列具有 48 位指针,由环、段和偏移量组成.大多数用户(在环 11 中)的空指针为 0xB00000000000.在旧的 CDC 补码机器上,使用全 1 位字作为各种数据(包括无效地址)的特殊标志是很常见的.

The CDC Cyber 180 Series has 48-bit pointers consisting of a ring, segment, and offset. Most users (in ring 11) have null pointers of 0xB00000000000. It was common on old CDC ones-complement machines to use an all-one-bits word as a special flag for all kinds of data, including invalid addresses.

旧的 HP 3000 系列对字节地址使用与字地址不同的寻址方案;像上面的几台机器一样,它对 char * 和 void * 指针使用不同的表示形式,而不是其他指针.

The old HP 3000 series uses a different addressing scheme for byte addresses than for word addresses; like several of the machines above it therefore uses different representations for char * and void * pointers than for other pointers.

Symbolics Lisp Machine 是一种标记架构,它甚至没有传统的数字指针;它使用这对(基本上是一个不存在的句柄)作为 C 空指针.

The Symbolics Lisp Machine, a tagged architecture, does not even have conventional numeric pointers; it uses the pair (basically a nonexistent handle) as a C null pointer.

根据使用的内存模型",8086 系列处理器(PC兼容)可以使用 16 位数据指针和 32 位函数指针,反之亦然.

Depending on the ``memory model'' in use, 8086-family processors (PC compatibles) may use 16-bit data pointers and 32-bit function pointers, or vice versa.

某些 64 位 Cray 机器在 a 的低 48 位中表示 int *单词;char * 另外使用一些高 16 位来表示字内的字节地址.

Some 64-bit Cray machines represent int * in the lower 48 bits of a word; char * additionally uses some of the upper 16 bits to indicate a byte address within a word.

附加链接:来自 Chris Torek 的消息,其中包含更多详细信息关于其中一些机器.

Additional links: A message from Chris Torek with more details about some of these machines.

这篇关于是否有任何平台指向不同类型的指针具有不同的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆