【问题标题】:what does enum DescriptorType DescriptorType :8; mean in C code?什么 enum DescriptorType DescriptorType :8;在C代码中是什么意思?
【发布时间】:2019-08-26 06:11:53
【问题描述】:

这是代码的一部分。看不懂是什么意思

enum DescriptorType DescriptorType :8;

是什么意思?

还有__attribute__ ((__packed__));在这里是什么意思?

enum DeviceClass {
 DeviceClassInInterface = 0x00,
 DeviceClassCommunications = 0x2,
 DeviceClassHub = 0x9,
 DeviceClassDiagnostic = 0xdc,
 DeviceClassMiscellaneous = 0xef,
 DeviceClassVendorSpecific = 0xff,
 };
struct UsbDeviceDescriptor {
    u8 DescriptorLength; // +0x0                                                                                                                                           
    enum DescriptorType DescriptorType : 8; // +0x1                                                                                                                        
    u16 UsbVersion; // (in BCD 0x210 = USB2.10) +0x2                                                                                                                       
    enum DeviceClass Class : 8; // +0x4                                                                                                                                    
    u8 SubClass; // +0x5                                                                                                                                                   
    u8 Protocol; // +0x6                                                                                                                                                   
    u8 MaxPacketSize0; // +0x7                                                                                                                                             
    u16 VendorId; // +0x8                                                                                                                                                  
    u16 ProductId; // +0xa                                                                                                                                                 
    u16 Version; // +0xc                                                                                                                                                   
    u8 Manufacturer; // +0xe                                                                                                                                               
    u8 Product; // +0xf                                                                                                                                                    
    u8 SerialNumber; // +0x10                                                                                                                                              
    u8 ConfigurationCount; // +0x11                                                                                                                                        
 } __attribute__ ((__packed__));

【问题讨论】:

  • 另外,属性 ((packed)) 是什么?在这里的意思?为什么它使用 attrubute__((__packed))?结构 UsbInterfaceDescriptor { InterfaceClassVendorSpecific = 0xff,} 类:8; // +x05 u8 子类; u8 协议; u8 字符串索引; } 属性 ((packed));
  • 请使用您的问题下方的edit 链接进行编辑。请记住,每个问题一个问题。
  • 至于你的问题,我建议你退后一步,买几本初学者的书,开始阅读结构和位域
  • __attribute__((__packed__)) 用于防止编译器在你的结构中插入无意义的位来指示对齐。
  • 这是非标准的胡言乱语。它很可能不会按照程序员的意图去做。该属性建议使用 gcc,但 gcc 不会跨不同类型填充位。最好的办法是重写整个代码。在您使用时也切换到stdint.h

标签: c struct enumeration bit-fields


【解决方案1】:
enum DescriptorType DescriptorType : 8; 

这是一个 8 位的位域。这表明该结构中包含枚举的 8 位。

C 中的 enum 变量具有实现定义的大小(参考 C99 标准中的 6.2.2.2)。如果您不将此作为位域包括在内,则可以使用超过 8 位来存储此值。

} __attribute__ ((__packed__));

这表明编译器不应在结构元素之间添加任何填充字节。通常,在不同的结构元素之间添加填充字节,以便更容易对齐 32 位或 16 位类型的访问。

您可以在有和没有__attribute__ ((__packed__)) 的情况下执行sizeof (struct UsbDeviceDescriptor),并且可以看到差异。

这是一个 GCC 特定的扩展。它也被其他一些编译器复制,但绝不是可移植的代码。

【讨论】:

  • 枚举位域也是编译器特定的。该标准只要求intunsigned int_Bool 在位域中工作。
  • 谢谢拉杰!那么这是否意味着设备上的每个项目都是 8 位? DeviceClassCommunications = 0x2,是否会保存为 DeviceClassCommunications = 0x02? DeviceClass { DeviceClassInInterface = 0x00, DeviceClassCommunications = 0x2, DeviceClassHub = 0x9, DeviceClassDiagnostic = 0xdc, DeviceClassMiscellaneous = 0xef, DeviceClassVendorSpecific = 0xff, };
  • "C 中的枚举与 int 类型相同" 否。枚举变量具有实现定义的大小。不要与始终为 int 的枚举常量混淆。
  • @BrianLee - 还有一些 16 位值。例如ProductIdVendorId
猜你喜欢
  • 2016-10-15
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多