挪用比特从指针 [英] stealing bits from a pointer

查看:99
本文介绍了挪用比特从指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多处理器编程,P215的艺术,作者说,在C,你可以从一个指针偷了一下,并使用位运算符提取一个字的一些标志(标记)和指针。我不知道这是怎么完成的,因此一个例子会帮助我。

In The Art of Multiprocessor Programming, p215, the authors say that in C, you could "steal" a bit from a pointer, and using bit-wise operators extract some flag (a mark) and the pointer from a single word. I don't know how this is done, so an example would help me.

推荐答案

想象一下,一个系统,一个32位指针大小,但只有1GB的内存可用。你只需要30位寻址整个内存空间,因此高2位未使用。您可以使用这些高两位为自己的目的 - 例如,以标记指针类型(堆栈/全局和动态)指针

Imagine a system with a 32-bit pointer size, but only 1GB of memory is available. You need only 30 bits to address the entire memory space, so the upper 2 bits are unused. You can use these upper two bits for your own purposes - for example, to mark pointers by pointer type (stack/global vs. dynamic).

注意,code,你得到的结果大约是不可移植的,因为它得到。你需要非常熟悉上的code运行时CPU - 特别是,你需要知道,如果高位掉下来的时候从一个指针地址发送到地址总线

Note that the code that you get as the result is about as non-portable as it gets. You need to be intimately familiar with the CPU on which your code runs - specifically, you need to know if the upper bits get dropped when an address from the pointer is sent to the address bus.

这篇关于挪用比特从指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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