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?
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
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__"
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