Padding and alignment in CodeWarrior for MC1321x (HCS08)

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Padding and alignment in CodeWarrior for MC1321x (HCS08)

ソリューションへジャンプ
1,692件の閲覧回数
drdr
Contributor I

When creating an array of a bit field structure as in the example below, is it possible to fit two 4-bit array members into a single byte?

 

typedef struct {
  unsigned int my_location : 2;

  unsigned int my_WATCHDOG : 2;

} foo;

 

foo my_array[1000] = 0;

 

Is it possible to have the above result in 500 bytes of 1000 elements, each addressed by my_array[i] or will CW pad every element such that 1000 bytes will be used by the array?

 

Thanks for the help,

 

drdr

ラベル(1)
タグ(1)
0 件の賞賛
1 解決策
566件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee

No, not with a simple foo structure.

C requires that array elements have their own addresses, therefore in order to have such a array you will have to pack a single byte in C code.

 

typedef struct {
  unsigned char my_low_location : 2;

  unsigned char my_low_WATCHDOG : 2;

  unsigned char my_high_location : 2;

  unsigned char my_high_WATCHDOG : 2;

} foo;


foo my_array[500];

 

 

Alternatively, and you can also use an array of bytes and then work with macros to implement the accesses to have a direct indexed access.


Daniel

元の投稿で解決策を見る

0 件の賞賛
2 返答(返信)
567件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee

No, not with a simple foo structure.

C requires that array elements have their own addresses, therefore in order to have such a array you will have to pack a single byte in C code.

 

typedef struct {
  unsigned char my_low_location : 2;

  unsigned char my_low_WATCHDOG : 2;

  unsigned char my_high_location : 2;

  unsigned char my_high_WATCHDOG : 2;

} foo;


foo my_array[500];

 

 

Alternatively, and you can also use an array of bytes and then work with macros to implement the accesses to have a direct indexed access.


Daniel
0 件の賞賛
566件の閲覧回数
drdr
Contributor I

Hi Daniel,

 

Thanks for the tip.

 

drdr

0 件の賞賛