简体   繁体   English

当指针不在cpu寄存器中时,是否可以将两个高阶零字节的指针存储为WORD?

[英]Is it possible to store pointer with two higher-order zero bytes as a WORD when it's not in a cpu register?

On a system where size of a pointer is 4 bytes when the intention is to just address parts of memory that are addressable by two bytes(lower parts), is it possible to store the pointer as a two byte WORD when it's not in some cpu register? 在一个指针大小为4字节的系统上,如果打算只寻址可被两个字节(下半部分)寻址的内存部分,则可以将指针存储为两个字节的WORD(当它不在某些CPU中时)。寄存器? I don't see any way cause assuming we've got any WORD like one named "twoBytes" by declaring a pointer like: 我看不出任何原因,可以通过声明一个指针来假设我们拥有一个名为“ twoBytes”的单词,例如:

char * pointer = reinterpret_cast<char *>((unsigned int)(twoBytes))

We're introducing a whole new entity with 4 bytes that's gonna be saved as a 4-byte entity. 我们将引入一个带有4个字节的全新实体,该实体将另存为4个字节的实体。

Generally you can store however little information is needed to recover the original pointer value, so yes, you can, although it's outside the guarantees offered by the language (you need to be sure how your particular compiler treats reinterpret casts). 通常,您可以存储很少的信息来恢复原始指针值,所以可以,尽管可以,尽管它超出了语言提供的保证范围(您需要确保特定的编译器如何对待重新解释类型转换)。

However, in eg Windows the only thing you can be sure of is that the upper word of a 32-bit pointer is non-zero for user code (except for nullpointers). 但是,例如在Windows中,您唯一可以确定的是用户代码的32位指针的高位字非零 (空指针除外)。 This is implicit in the Windows API macros like MAKEINTATOM . 这在MAKEINTATOM之类的Windows API宏中是隐式的。 If the most significant word could be zero then the APIs couldn't reliably distinguish pointers that represent small integers, from pointers to text strings. 如果最高有效字可能为零,则API无法可靠地将代表小整数的指针与指向文本字符串的指针区分开。

So, in general, optimizing that way won't buy you anything unless you're doing kernel programming. 因此,总的来说,除非您正在执行内核编程,否则以这种方式进行优化不会为您带来任何好处。 Also, saving a few bytes is seldom worth the added complexity. 同样,节省几个字节很少值得增加复杂性。

Cheers & hth., 干杯,……

What you are describing sounds more like a compiler feature (good old fashioned "near" pointers) than something you can do from inside the language. 您所描述的内容听起来更像是一种编译器功能(老式的“接近”指针),而不是您可以从语言内部完成的操作。 Take it up with whoever made the compiler you're using. 与使用您所使用的编译器的人接洽。 I can vouch for the theoretical possibility of being able to implement this behavior in GCC, although I suspect it would be a huge pain in the ass. 我可以保证能够在GCC中实现这种行为的理论可能性 ,尽管我怀疑这将是一个巨大的痛苦。

As an alternative hack, you might be able to get most of what you want using a base pointer and 'unsigned short' offsets. 作为一种替代方法,您可以使用基本指针和“无符号短”偏移量来获取所需的大部分内容。

No, for the same reason you can't store the word "bike" in two bytes. 不,出于相同的原因,您不能将单词“ bike”存储在两个字节中。 The data just won't fit. 数据就不合适了。 Don't cast pointers to non-pointer types, it's often non-portable and can silently introduce truncation and cause some nasty bugs. 不要将指针转换为非指针类型,它通常是不可移植的,并且可以无声地引入截断并引起一些讨厌的错误。

You can also use it without a named 4 byte entity: 您也可以在没有命名的4字节实体的情况下使用它:

((char*)(unsigned)twoBytes)[idx] = some_val;

twoBytes will only take up two bytes in memory. twoBytes仅占用内存中的两个字节。 When you cast it to a char*, your compiler will make a 4-byte value to actually address the data, but you'll never see it, and it will likely only ever be in a register. 当将其强制转换为char *时,编译器将为实际寻址数据提供一个4字节的值,但您将永远看不到它,并且它很可能只在寄存器中。 I think that's what you were asking. 我想这就是你要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM