Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用位字段编写C结构_C_Windows_Structure - Fatal编程技术网

使用位字段编写C结构

使用位字段编写C结构,c,windows,structure,C,Windows,Structure,我要转换此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 Pool

我要转换此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个字节