Hello,
The problem arise only using an unsigned long in a struct and only with a struct size greater than a specific threashold ( 9? )
Compilers tested
16.9.10 LTS
20.2.5 LTS
example:
in the working code, outsideVar has address: 0x00008306
This is correct, since the previus variable w3 has address: 0x00008305
In the code with bug, outsideVar has address: 0x00008320
This is not correct, since the previus variable w10 has address: 0x0000831E
So compier let 0x0000831F blank. However you can see this address in the variable "all", so this behavior can be dangerus for memcopy routine
Code working fine
typedef union _TMP
{
struct
{
union
{
struct
{
unsigned short w0;
unsigned short w1;
unsigned short w2;
unsigned short w3;
} EXPANDED;
struct
{
unsigned long w0_w1;
unsigned short w2_w3[2];
} COMPRESSED;
unsigned short all[4];
} SUBSYSTEM;
unsigned short outsideVar;
}expand;
unsigned short all[5];
} TMP;
Code with bug:
typedef union _TMP
{
struct
{
union
{
struct
{
unsigned short w0;
unsigned short w1;
unsigned short w2;
unsigned short w3;
unsigned short w4;
unsigned short w5;
unsigned short w6;
unsigned short w7;
unsigned short w8;
unsigned short w9;
unsigned short w10;
} EXPANDED;
struct
{
unsigned long w0_w1;
unsigned short w2_w18[9];
} COMPRESSED;
unsigned short all[11];
} SUBSYSTEM;
unsigned short outsideVar;
}expand;
unsigned short all[12];
} TMP;