Padding and alignment in CodeWarrior for MC1321x (HCS08)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Padding and alignment in CodeWarrior for MC1321x (HCS08)

Jump to solution
1,662 Views
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

Labels (1)
Tags (1)
0 Kudos
1 Solution
536 Views
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

View solution in original post

0 Kudos
2 Replies
537 Views
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 Kudos
536 Views
drdr
Contributor I

Hi Daniel,

 

Thanks for the tip.

 

drdr

0 Kudos