请帮我看下这个type占几个字节?

wuwenzhe 2012-11-30 10:01:56

typedef union
{
u32 value;
struct
{
u32 seconds :6;
u32 minutes :6;
u32 hours :5;
u32 days :15; // from 01/01/2000
} info;
} t32_date_time;



typedef union
{
u32 value;
struct
{
u32 ignition :1; // (0x00 off, 0x01 on)
u32 panic :1; // (0x00 no panic status, 0x01 panic status)
u32 input1 :1; // (0x00 open, 0x01 low)
u32 input2 :1; // (0x00 open, 0x01 low)
u32 input3 :2; // (0x00 open, 0x01 low, 0x02 high)
u32 input4 :1; // (0x00 open, 0x01 high)
u32 output1 :1; // (0x00 open, 0x01 grounded)

u32 output2 :1; // (0x00 open, 0x01 grounded)
u32 output3 :1; // (0x00 open, 0x01 high)
u32 direction :3; // (0x00 north, 0x01 north east, 0x02 east, 0x03 south east, 0x04 south, 0x05 south west, 0x06 west, 0x07 north west)
u32 gprs_connection :1; // (0x00 no server connection, 0x01 connected to server)
u32 voice_call :1; // (0x00 no voice call, 0x01 voice call)
u32 gps_fix :1; // (0x00 valid position retrieved from memory, 0x01 valid position actual)

u32 gps_antenna_fail :1; // (0x00 antenna OK, 0x01 antenna fail)
u32 max_speed_exceeded :1; // (0x00 speed OK, 0x01 max speed exceeded)
u32 tampering_seosor :1; // (0x00 tampering is OK, 0x01 tampering is open)
u32 sleep :1; // (0x00 GPS not sleeping, 0x01 will enter sleep and turn off GPS)
u32 battery_charging :1; // (0x00 not charging, 0x01 charging)
u32 battery_fault :1; // (0x00 internal battery OK, 0x01 battery not found)
u32 power_fail :1; // (0x00 power ok, 0x01 no external power)
u32 waypoint_restricted :1; // (0x00 off, 0x01 on)

u32 waypoint_entering :1; // (entering waypoint)
u32 waypoint_leaving :1; // (leaving waypoint)
u32 GSM_jamming :1; // (0x00 no jamming, 0x01 jamming)
u32 anti_theft_status :2; // (0x00 disarmed, 0x01 armed, 0x02 suspended , 0x03 alarmed)
u32 accessory_missing :1; //at least one expected accessory not found
u32 moving_status :1; // (0x00 stopped, 0x01 moving)
u32 cell_id_present :1; // (CC, NC, LC, ID - 8 bytes – will be present)
} info;
} t32_flags;



主要我不知道这两个union中的第一行u32 value;占不占4字节空间?
...全文
278 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2012-11-30
  • 打赏
  • 举报
回复
bit-field 冒号后面表示占几个二进制位。 加起来都是32位,
引用 13 楼 wuwenzhe 的回复:
引用 12 楼 ForestDB 的回复:value是4,info也是4。 那还有什么问题? 还是说LZ不理解 u32 seconds :6; 是的,不理解这行
wuwenzhe 2012-11-30
  • 打赏
  • 举报
回复
引用 12 楼 ForestDB 的回复:
value是4,info也是4。 那还有什么问题? 还是说LZ不理解 u32 seconds :6;
是的,不理解这行
ForestDB 2012-11-30
  • 打赏
  • 举报
回复
value是4,info也是4。 那还有什么问题? 还是说LZ不理解 u32 seconds :6;
wuwenzhe 2012-11-30
  • 打赏
  • 举报
回复
引用 10 楼 ForestDB 的回复:
union中的struct,即代码中的info,LZ觉得它的size是多少? 为了考察LZ是否了解union,LZ认为下面的union的size是多少? union { int a; int b; };
当然是4了,我问的问题涉及到内存对齐的问题吧?
ForestDB 2012-11-30
  • 打赏
  • 举报
回复
union中的struct,即代码中的info,LZ觉得它的size是多少? 为了考察LZ是否了解union,LZ认为下面的union的size是多少? union { int a; int b; };
nice_cxf 2012-11-30
  • 打赏
  • 举报
回复
已经跟你说了是4字节,union是公用同样的地址空间,那个结构也是4字节
mujiok2003 2012-11-30
  • 打赏
  • 举报
回复
u32如果名副其实的话,是4个字节。
wuwenzhe 2012-11-30
  • 打赏
  • 举报
回复
顶一下,求大牛帮忙
wuwenzhe 2012-11-30
  • 打赏
  • 举报
回复
引用 4 楼 ForestDB 的回复:
两个union中的value和info都是32bit,即4byte。 由于是union,所以t32_date_time和t32_flags都是4byte。
但是union里边还包含struct的呀,struct还有好多字节不算在union里边吗?
ForestDB 2012-11-30
  • 打赏
  • 举报
回复
value和info虽然都是4byte,但是它们是共用的。
ForestDB 2012-11-30
  • 打赏
  • 举报
回复
两个union中的value和info都是32bit,即4byte。 由于是union,所以t32_date_time和t32_flags都是4byte。
mymtom 2012-11-30
  • 打赏
  • 举报
回复
引用 2 楼 wuwenzhe 的回复:
我的意思是:t32_date_time和t32_flags都分别占多少字节,不是问u32代表多少字节
t32_date_time和t32_flags确实都是4字节嘛
wuwenzhe 2012-11-30
  • 打赏
  • 举报
回复
我的意思是:t32_date_time和t32_flags都分别占多少字节,不是问u32代表多少字节
mymtom 2012-11-30
  • 打赏
  • 举报
回复
好像都是4字节嘛

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧