Page 1 of 1
SizeOf and nested structures
Posted: Fri May 14, 2021 3:55 pm
by GPI
Code: Select all
Structure t1
l.l
a.a
EndStructure
Structure t2
d.d
b.t1
EndStructure
a.t2
Debug SizeOf(a)
Debug SizeOf(a\b)
Debug SizeOf(a\b\a) ; <- Syntax error?!
Debug SizeOf(t2)
Debug SizeOf(t2\b)
Debug SizeOf(t2\b\a); <- dito
Re: SizeOf and nested structures
Posted: Fri May 14, 2021 10:49 pm
by STARGĂ…TE
Would you call it a bug? I would say it is a missing feature.
From the documentation it is not clear, if SizeOf also allows nested structure calls insteat of just the field of the present structure.
A similar "missing feature" is the OffsetOf() of nested structures.
Here you need an addition:
Code: Select all
Debug OffsetOf(t2\b) + OffsetOf(t1\a) ; Debug OffsetOf(t2\b\a)
Re: SizeOf and nested structures
Posted: Sat May 15, 2021 8:32 am
by GPI
the problem is, i used it at the moment to identify a structure dynamicaly.
Code: Select all
Structure ab
StructureUnion;structure-union is the trick here
_type.b[1]
somedata.l
EndStructureUnion
EndStructure
Structure bc
StructureUnion
_type.b[2]
elsedata.l
EndStructureUnion
EndStructure
Procedure ab_doit(*a.ab)
Debug "AB-Variant"
EndProcedure
Procedure bc_doit(*b.bc)
Debug "BC-Variant"
EndProcedure
Macro GetType(x): SizeOf(x\_type) :EndMacro
Macro doit(x)
CompilerSelect GetType(x)
CompilerCase gettype(ab)
ab_doit(x)
CompilerCase gettype(bc)
bc_doit(y)
CompilerDefault
CompilerError "[doit] Supported type"
CompilerEndSelect
EndMacro
Define var.ab, ok.bc
doit(var)
doit(ok)
The good thing with this method is, that it is done on compiler-level without any additional code or memory used. Very Usefull, when you work with matrix and vectors...
As long as you don't use nested structures everything works perfectly.
Re: SizeOf and nested structures
Posted: Sun May 14, 2023 8:48 pm
by Psychophanta
Yes please, allow nested structures for SizeOf(), the C of the arduino does allow it.
I know, it might be hard but very useful at the end.
Another fast example:
Code: Select all
structure pepe
jose.f
juan.d
endstructure
Structure Person
Name.s
ForName.s
Age.pepe
EndStructure
var.person
Debug sizeof(person\Age); <- if this is allowed ...
;Debug sizeof(person\Age\juan); <- why this one is not?. At least it is not a "Syntax error" !