OffsetOf, SizeOf, TypeOf inconsistency and feature request

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

OffsetOf, SizeOf, TypeOf inconsistency and feature request

Post by NicTheQuick »

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:

Code: Select all

Structure foo
	bar.l[4]
EndStructure

Debug OffsetOf(foo\bar) ; = 0
But this would be cool and is not possible at the moment (6.01 LTS beta 4):

Code: Select all

Structure foo
	bar.l[4]
EndStructure

Debug OffsetOf(foo\bar[2])
This is a workaround using a dummy variable:

Code: Select all

Structure foo
	bar.l[4]
EndStructure

Define dummy.foo
Debug @dummy\bar[2] - @dummy ; = 8
But then you would also think you can use OffsetOf() in conjunction with SizeOf() to get rid of that dummy variableoff like so:

Code: Select all

Structure foo
	bar.l[4]
EndStructure

Debug OffsetOf(foo\bar) + 2 * SizeOf(foo\bar) ; = 32
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:

Code: Select all

Structure foo
	bar.l[4]
EndStructure

Debug TypeOf(foo\bar) ; = #PB_Long
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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Splunk
User
User
Posts: 36
Joined: Wed Apr 21, 2021 6:53 pm

Re: OffsetOf, SizeOf, TypeOf inconsistency and feature request

Post by Splunk »

Thumbs up!

In my opinion, the parameter "TypeOf()" is completely superfluous for SortStructuredList() (except for a highly rare special case). With the parameter "OffsetOf()" PB itself can determine which type it is.
User avatar
ccode_new
User
User
Posts: 21
Joined: Sat Jul 30, 2022 10:39 am

Re: OffsetOf, SizeOf, TypeOf inconsistency and feature request

Post by ccode_new »

Splunk wrote: Wed Mar 01, 2023 8:03 pm With the parameter "OffsetOf()" PB itself can determine which type it is.
Are you sure?
Splunk
User
User
Posts: 36
Joined: Wed Apr 21, 2021 6:53 pm

Re: OffsetOf, SizeOf, TypeOf inconsistency and feature request

Post by Splunk »

yes, e.g. 2 times the same indication "foo\bar" and because it is logical that if I want to sort "foo\bar", I also use the same TypeOf "foo\bar". Everything else makes no sense (in the normal case).:

Code: Select all

SortStructuredList(List(), Options, OffsetOf(foo\bar), TypeOf(foo\bar))
Post Reply