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

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

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

3,967 Views
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?

Labels (1)
Tags (1)
0 Kudos
Reply
3 Replies

2,314 Views
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 Kudos
Reply

2,314 Views
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 Kudos
Reply

2,314 Views
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 Kudos
Reply