OffsetOf, SizeOf, TypeOf inconsistency and feature request
Posted: Wed Mar 01, 2023 7:40 pm
Hi there,
as I found out in an other topic there is no possibility to calculate the offset of static array members other than creating the difference to the first element in the structure if you use a dummy variable or an already existing instance of a variable with that structure.
This of course works:
But this would be cool and is not possible at the moment (6.01 LTS beta 4):
This is a workaround using a dummy variable:
But then you would also think you can use OffsetOf() in conjunction with SizeOf() to get rid of that dummy variableoff like so:
Huh? SizeOf(foo\bar) returns the size of the whole static array instead of the single member. This somehow makes sense. But now let's check TypeOf:
The member seemingly is a Long but then its size should be 4 and not 16.
Conclusion
So it is not possible to calculate the correct offset to a certain static array member when you want to use it together with SortStructuredList for example. Instead you need to use magic numbers or you have to know the type of your field in other places in your code or you need a dummy variable to be able to calculate it more accurate.
The simplest solution in my opinion to solve that problem would be to add the possibility for square brackets within the structure member path like I demonstrated it in the second code snippet.
as I found out in an other topic there is no possibility to calculate the offset of static array members other than creating the difference to the first element in the structure if you use a dummy variable or an already existing instance of a variable with that structure.
This of course works:
Code: Select all
Structure foo
bar.l[4]
EndStructure
Debug OffsetOf(foo\bar) ; = 0
Code: Select all
Structure foo
bar.l[4]
EndStructure
Debug OffsetOf(foo\bar[2])
Code: Select all
Structure foo
bar.l[4]
EndStructure
Define dummy.foo
Debug @dummy\bar[2] - @dummy ; = 8
Code: Select all
Structure foo
bar.l[4]
EndStructure
Debug OffsetOf(foo\bar) + 2 * SizeOf(foo\bar) ; = 32
Code: Select all
Structure foo
bar.l[4]
EndStructure
Debug TypeOf(foo\bar) ; = #PB_Long
Conclusion
So it is not possible to calculate the correct offset to a certain static array member when you want to use it together with SortStructuredList for example. Instead you need to use magic numbers or you have to know the type of your field in other places in your code or you need a dummy variable to be able to calculate it more accurate.
The simplest solution in my opinion to solve that problem would be to add the possibility for square brackets within the structure member path like I demonstrated it in the second code snippet.