gpt4 book ai didi

c - 使用位域编写 C 结构

转载 作者:太空宇宙 更新时间:2023-11-04 03:36:12 24 4
gpt4 key购买 nike

我想转换这个 8 字节的结构:

nt!_POOL_HEADER
+0x000 PreviousSize : Pos 0, 9 Bits
+0x000 PoolIndex : Pos 9, 7 Bits
+0x002 BlockSize : Pos 0, 9 Bits
+0x002 PoolType : Pos 9, 7 Bits
+0x000 Ulong1 : Uint4B
+0x004 PoolTag : Uint4B
+0x004 AllocatorBackTraceIndex : Uint2B
+0x006 PoolTagHash : Uint2B

进入这样的 C 结构:

struct _POOL_HEADER {
PreviousSize; // what SIZE do i Need to specify here ?
PoolIndex; // what SIZE do i Need to specify here ?
BlockSize; // what SIZE do i Need to specify here ?
PoolType; // what SIZE do i Need to specify here ?
unsigned int Ulong1;
unsigned int PoolTag;
unsigned short AllocatorBackTraceIndex;
unsigned short PoolTagHash;
};

我知道这可以用位域来完成,但是怎么做呢?这是在 x86 上。

最佳答案

假设 int 是 32 位,这会做:

struct _POOL_HEADER {
int PreviousSize :9;
int PoolIndex :7;
int BlockSize :9;
int PoolType :7;
...

上面的部分使用了 4 个字节。

如果你想坚持8个字节,你需要改变下半部分,

  unsigned int Ulong1;
unsigned int PoolTag;
unsigned short AllocatorBackTraceIndex;
unsigned short PoolTagHash;
};

因为您的提案使用了超过剩余的 4 个字节,即 12 个字节。

关于c - 使用位域编写 C 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32548043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com