structure data alignment rules (56F8013)

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

structure data alignment rules (56F8013)

Jump to solution
5,599 Views
BeerBomber
Contributor I

Hi,

I'm using a 56F8013 and I would like to control the compiler's boundary rule for structure. In "Targeting_56800E.pdf" at section "Targeting MC56F83xx/DSP5685x Controllers" page 133, it says that "A structure containing only bytes still is word aligned.".

The problem is that I'm trying to read an array of single-byte structure and I would really like to use the structure layout in my C code.

If I have an array of these then
typedef struct{
unsigned char _someValueA:3;
unsigned char _someValueB:2;
unsigned char _someValueC:1;
unsigned char _someValueD:1;
unsigned char _someValueE:1;
}myStruct;

I can't use things like ptr->_someValueC because of code generated forces the boundary of the structure to be word aligned. I need it to be byte aligned. Is there any ways (pragmas, attribute or compiler flags) that I can use my structure in C?

Otherwise I have to read them as byte and use explicit shifter which reduce the clarity of the source.

I'm using :
Metrowerks C/C++ for DSP M56800E.
Copyright (c)1998-2005, Metrowerks
All rights reserved.
Version 7.2 build 1003
Runtime Built: Apr 15 2005 19:46:26

Thanks.

Labels (1)
0 Kudos
Reply
1 Solution
2,688 Views
pittbull
Contributor III
>> Otherwise I have to read them as byte and use explicit shifter which
>> reduce the clarity of the source.

Hello BeerBomber,
Using structs and bitfields to interchange data between two different systems often causes weird problems. Compilers are free to align structures as they like and put dummy data between members. Better store all that stuff into byte arrays and use shift/logical operators to access them. You can write some macros or inline functions for that which keeps your code pretty...

View solution in original post

0 Kudos
Reply
2 Replies
2,689 Views
pittbull
Contributor III
>> Otherwise I have to read them as byte and use explicit shifter which
>> reduce the clarity of the source.

Hello BeerBomber,
Using structs and bitfields to interchange data between two different systems often causes weird problems. Compilers are free to align structures as they like and put dummy data between members. Better store all that stuff into byte arrays and use shift/logical operators to access them. You can write some macros or inline functions for that which keeps your code pretty...
0 Kudos
Reply
2,688 Views
BeerBomber
Contributor I
Thanks for the quick answer,


I understand the need for word alignment even for simple byte(s) structure (whatever I use bitfields or not) for performance reasons when creating these structures.

I'm receive a stream of packed byte structure and I just wished that I could control the way the code was generated in the cases where I'm using byte pointers to access my structure. Ha well, I'm back with macros.
0 Kudos
Reply