Attribute "size" corrupts variables in structs

The template special attribute “size” makes problems when used e.g. in a structure.

local uint var1;
local uint var2;
local uint var3;

var1 = 0x1111;
var2 = 0x2222;
var3 = 0x3333;

Printf( "Before struct test1, var1=%X var2=%X var3=%X\n", var1, var2, var3 );
struct {
    Printf( "Inside struct test1, var1=%X var2=%X var3=%X\n", var1, var2, var3 );
    UBYTE testbyte1;
} test1 <size = 1>; // with "size" attribute
//} test1; // without "size" attribute

var1 = 0x44444444;
var2 = 0x55555555;
var3 = 0x66666666;

Printf( "Before struct test2, var1=%X var2=%X var3=%X\n", var1, var2, var3 );
struct {
    Printf( "Inside struct test2, var1=%X var2=%X var3=%X\n", var1, var2, var3 );
    UBYTE testbyte2;
} test2 <size = 1>; // with "size" attribute
//} test2; // without "size" attribute

Printf( "Now var1 var2 var3 are not used anymore...\n" );
var1 = 0xDEADBEEF;
var2 = 0xDEADBEEF;
var3 = 0xDEADBEEF;

With this testcode, and as no variables are altered inside the structure, you would expect an output like this:

Before struct test1, var1=1111 var2=2222 var3=3333
Inside struct test1, var1=1111 var2=2222 var3=3333
Before struct test2, var1=44444444 var2=55555555 var3=66666666
Inside struct test2, var1=44444444 var2=55555555 var3=66666666
Now var1 var2 var3 are not used anymore...

But when you expand both test structures, you instead get corrupted variables like this:

Before struct test1, var1=1111 var2=2222 var3=3333
Inside struct test1, var1=DEADBEEF var2=DEADBEEF var3=DEADBEEF
Before struct test2, var1=44444444 var2=55555555 var3=66666666
Inside struct test2, var1=DEADBEEF var2=DEADBEEF var3=DEADBEEF
Now var1 var2 var3 are not used anymore...

And code that uses variables throughout the template like this gets corrupted variables inside structures. But without the “size” attribute in the structure, you will get normal behaviour. Can this be fixed?

This is intentional, from what I understand. When attributes are used, processing of the struct is deferred to after the template has finished running. The value of var1/var2/var3 are all 0xDEADBEEF once the template is finished, so that is what gets printed.

Yes, I think I get it. So it becomes a separate task/thread that no longer participates in the linear process. It needs to have its own set of variables in order to communicate with other parts of the code:

local uint var1;
local uint var2;
local uint var3;
local uint test1var1;
local uint test1var2;
local uint test1var3;
local uint test2var1;
local uint test2var2;
local uint test2var3;

var1 = 0x1111;
var2 = 0x2222;
var3 = 0x3333;
test1var1 = var1;
test1var2 = var2;
test1var3 = var3;

Printf( "Before struct test1, var1=%X var2=%X var3=%X\n", var1, var2, var3 );
struct {
    Printf( "Inside struct test1, test1var1=%X test1var2=%X test1var3=%X\n", test1var1, test1var2, test1var3 );
    UBYTE testbyte1;
} test1 <size = 1>;

var1 = 0x44444444;
var2 = 0x55555555;
var3 = 0x66666666;
test2var1 = var1;
test2var2 = var2;
test2var3 = var3;

Printf( "Before struct test2, var1=%X var2=%X var3=%X\n", var1, var2, var3 );
struct {
    Printf( "Inside struct test2, test2var1=%X test2var2=%X test2var3=%X\n", test2var1, test2var2, test2var3 );
    UBYTE testbyte2;
} test2 <size = 1>;

Printf( "Now var1 var2 var3 are not used anymore...\n" );
var1 = 0xDEADBEEF;
var2 = 0xDEADBEEF;
var3 = 0xDEADBEEF;

Finally:

Before struct test1, var1=1111 var2=2222 var3=3333
Before struct test2, var1=44444444 var2=55555555 var3=66666666
Now var1 var2 var3 are not used anymore...
Inside struct test1, test1var1=1111 test1var2=2222 test1var3=3333
Inside struct test2, test2var1=44444444 test2var2=55555555 test2var3=66666666

That has helped a lot to clear things up. Thank you.