AnsweredAssumed Answered

How to align bytes in CW10.6 for DSC 56F84xxx?

Question asked by Sutter Zhou Employee on Dec 29, 2016
Latest reply on Jan 16, 2017 by ZhangJennie

When defining global variables in such way:

byte var1;

byte var2;

byte var3;

byte varArray[10];

After compiling, var1 is placed at the lower 8 bits of address 0x00, var2 is placed at the upper 8 bits of address 0x00, var3 is placed at the lower 8 bits of address 0x01, and varArray is located from the upper 8 bits of address 0x01. 


If there's a structure pointer such as:

typedef struct
byte w8Data1;
byte w8Data2;
byte w8Data3;
byte w8Data4;
byte w8Data5;
byte w8Data6;
byte w8Data7;
byte w8Data8;
word w16Data10;


STRUCT_TEST *sPtr = (STRUCT_TEST *)varArray;


now sPtr->w8Data1 is actually the lower 8 bits of address 0x01, which is var3, but not varArray[0]. The reason is that varArray is not aligned and a structure pointer is always a word pointer in DSC compiler. So I tried to find a flexible way to do the alignment without modifying linker file. I found there's a similar question in this community, and the answer is to define variables in such way:

 byte varArray[10]  __attribute__((aligned(2))); 

But it is NOT working at all in CW10.6 on 56F84xxx.


Is there a solution for this? I need the answer asap.