HowTo force packing data structure with size less than 4 bytes?

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

HowTo force packing data structure with size less than 4 bytes?

4,651件の閲覧回数
ITCO
Contributor III

Hi,

I'm working on MSC8156 CW 10.1.x.

I would like to define data structure that contains 16 bit field. By default it occupies 4 bytes in the memory. How do I force this structure to occupy its actual size, 16 bits?

ラベル(1)
タグ(1)
0 件の賞賛
返信
3 返答(返信)

2,998件の閲覧回数
CrasyCat
Specialist III

Hello

 

You can use __attribute__ packed for that in the compiler.

 

For instance

 

typedef struct  __attribute__((packed, aligned(2))) m {

    unsigned short m0;

    unsigned int   m1;

} m_struct ;

 

m_struct m_var;

 

This will remove padding between the field in the structure and will ensure each field is aligned to a 2 bytes boundary.

 

With this notation if you change your structure to typedef struct  __attribute__((packed, aligned(2))) m {

    unsigned char  m0;

    unsigned int   m1;

} m_struct ;

 

There will be 1 byte padding between m0 and m1 and the structure size will be 6 bytes.

 

If you do not want any padding between the fields, you can use __attribute__ ((packed)) alone.

 

With the notation

typedef struct  __attribute__((packed)) m {

    unsigned char  m0;

    unsigned int   m1;

} m_struct ;

 

There will be no padding between m0 and m1 and the size of the structure will be 5 bytes.

 

I hope this helps

 

CrasyCat

0 件の賞賛
返信

2,998件の閲覧回数
ignisuti
Contributor IV

CrazyCat, I'm trying to accomplish the same thing, but am getting an error in CW 10.1 with MCU MCF51CN128.

 

I copied and pasted the same struct:

typedef struct  __attribute__((packed)) m {    unsigned char  m0;    unsigned int   m1;} m_struct ;

 Here is the error I'm seeing: 

"illegal or unsupported __attribute__"

0 件の賞賛
返信

2,998件の閲覧回数
CrasyCat
Specialist III

Hello

 

This forum is dedicated to CodeWarrior for StarCore.

If you have a question on CodeWarrior for Coldfire, please post it under the appropriate forum.

You will not get any answer to a coldfire question in a post dedicated to StarCore development tool.

 

Thanks for your understanding.

 

CrasyCat

0 件の賞賛
返信