We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I'm using C51 v3.20, and when I compiled the following structure, the compiler allocates 6 bytes for it.
typedef struct t_channel_info { int target_temp:9; unsigned char control_mode:2; unsigned char status:2; unsigned char warming:1; int current_temp:9; unsigned char warm_proportion; } t_channel_info;
Thanks for your kind replies. Based on your comments and links, I modified the struct as follows:
typedef struct tt_channel_info { unsigned int warm_proportion:7; int target_temp:9; unsigned int control_mode:2; unsigned int status:2; unsigned int warming:1; int current_temp:9; } tt_channel_info; tt_channel_info. . . . . . . . . . . . TYPEDEF ----- STRUCT ----- 4 warm_proportion. . . . . . . . . . . MEMBER ----- FIELD 0000H 7.0 target_temp. . . . . . . . . . . . . MEMBER DATA FIELD 0000H 9.7 control_mode . . . . . . . . . . . . MEMBER DATA FIELD 0002H 2.0 status . . . . . . . . . . . . . . . MEMBER DATA FIELD 0002H 2.2 warming. . . . . . . . . . . . . . . MEMBER DATA FIELD 0002H 1.4 current_temp . . . . . . . . . . . . MEMBER DATA FIELD 0002H 9.5
Since you have control over the layout, I suggest the following modification to reduce code size and increase speed.
typedef struct tt_channel_info { int target_temp:9; //keep the fields > 8 bits at int offset 0 unsigned int warm_proportion:7; int current_temp:9; //keep the fields > 8 bits at int offset 0 unsigned int control_mode:2; unsigned int status:2; unsigned int warming:1; } tt_channel_info;
hmm, thanks for the hint