指向不同类型的指针可以具有不同的二进制表示形式吗? [英] Can pointers to different types have different binary representations?

查看:55
本文介绍了指向不同类型的指针可以具有不同的二进制表示形式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否允许C ++实现以不同的方式表示指向不同类型的指针.例如,如果我们有4字节大小/对齐的 int 和8字节大小/对齐的 long ,则可以表示指向 int的指针/ long 作为对象地址分别右移2/3位?这将有效地禁止将指向 long 的指针转换为指向 int 的指针.

I wonder if C++ implementations are allowed to represent pointers to different types differently. For instance, if we had 4-byte sized/aligned int and 8-byte sized/aligned long, would it be possible to represent pointers-to-int/long as object addresses shifted right by 2/3 bits, respectively? This would effectively forbid to convert a pointer-to-long into a pointer-to-int.

我问是因为 [expr.reinterpret.cast/7] :

对象指针可以显式转换为其他类型的对象指针.将对象指针类型的prvalue v 转换为对象指针类型指针"时,到 cv T ",结果为 static_cast< cv T *>(static_cast< cv void *>(v)).

An object pointer can be explicitly converted to an object pointer of a different type. When a prvalue v of object pointer type is converted to the object pointer type "pointer to cv T", the result is static_­cast<cv T*>(static_­cast<cv void*>(v)).

[注释7 :将类型为" T1 的指针"的指针转换为类型为" T1 "的对象的指针.指向 T2 的指针"(其中 T2 是对象类型,并且 T2 的对齐要求并不严格于T1 )并返回其原始类型将产生原始指针值.— 尾注]

[Note 7: Converting a pointer of type "pointer to T1" that points to an object of type T1 to the type "pointer to T2" (where T2 is an object type and the alignment requirements of T2 are no stricter than those of T1) and back to its original type yields the original pointer value. — end note]

第一句话建议我们可以将指针转换为任何两种对象类型.但是,注释7 中的(非规范性)同情文本表示对齐在这里也起了一定作用.(因此,我想到了上面的 int - long 示例.)

The first sentence suggests that we can convert pointers to any two object types. However, the empathized text in the (not normative) Note 7 then says that the alignment plays some role here as well. (That's why I came up with that int-long example above.)

推荐答案

作为一个具体示例,有一个C ++实现,其中指向单字节元素的指针大于指向多字节元素的指针,因为硬件使用字(而非字节)寻址.为了模拟字节指针,C ++使用了一个硬件指针加上一个额外的字节偏移量.

Yep

As a concrete example, there is a C++ implementation where pointers to single-byte elements are larger than pointers to multi-byte elements, because the hardware uses word (not byte) addressing. To emulate byte pointers, C++ uses a hardware pointer plus an extra byte offset.

void * 存储该额外的偏移量,但 int * 不存储.将 int * 转换为 char * 是可行的(必须在标准下),但是将 char * 转换为 int * 会丢失该偏移量(您的注释隐含允许).

void* stores that extra offset, but int* does not. Converting int* to char* works (as it must under the standard), but char* to int* loses that offset (which your note implicitly permits).

Cray T90超级计算机是一个示例这样的硬件.

The Cray T90 supercomputer is an example of such hardware.

我将看看是否可以找到标准参数来说明为什么这对于兼容的C ++编译器是有效的.我只知道有人这样做了,并不是说这样做是合法的,而是那条音符暗示它是合法的.

I will see if I can find the standards argument why this is valid thing for a compliant C++ compiler to do; I am only aware someone did it, not that it is legal to do it, but that note rather implies it is intended to be legal.

这些规则将在to-from void指针强制转换规则中.您引用的段落隐含了将转换的含义转发到那里.

The rules are going to be in the to-from void pointer casting rules. The paragraph you quoted implicitly forwards the meaning of the conversion to there.

指针指向cv1无效"类型的prvalue可以转换为指针指向cv2 T的类型"的prvalue,其中T是对象类型,并且cv2是与cv-qualification相同或大于cv-qualification的对象.,cv1.如果原始指针值表示内存中一个字节的地址A,而A不满足T的对齐要求,则未指定结果指针值.否则,如果原始指针值指向对象a,并且存在类型T(忽略cv限定)的对象b,该对象可与a进行指针互换,则结果是指向b的指针.否则,转换后指针值将保持不变.

A prvalue of type "pointer to cv1 void" can be converted to a prvalue of type "pointer to cv2 T", where T is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. If the original pointer value represents the address A of a byte in memory and A does not satisfy the alignment requirement of T, then the resulting pointer value is unspecified. Otherwise, if the original pointer value points to an object a, and there is an object b of type T (ignoring cv-qualification) that is pointer-interconvertible with a, the result is a pointer to b. Otherwise, the pointer value is unchanged by the conversion.

这表明转换为更多对齐的类型会生成未指定的指针,但是转换为实际上不存在的相等或更少的对齐类型不会更改指针值.

This demonstrates that converting to more-aligned types generates an unspecified pointer, but converting to equal-or-less aligned types that aren't actually there does not change the pointer value.

允许将指针转换为4字节对齐数据的指针转换为指针转换为8字节对齐数据的指针会导致垃圾.

Which is permission to make a cast from a pointer to 4 byte aligned data converted to a pointer to 8 byte aligned data result in garbage.

每个与对象无关的指针强制转换都需要在逻辑上往返于 void * .

Every object unrelated pointer cast needs to logically round-trip through a void* however.

可以将对象指针显式转换为其他类型的对象指针.当将对象指针类型的prvalue v转换为对象指针类型"cv T的指针"时,结果为 static_cast< cv T *>(static_cast< cv void *>(v)).

(来自OP)

涵盖从 void * T * 的内容;我尚未找到将 T * 转换为 void * 的转换文本,以使其成为完整的

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