Page 1 of 2
A good reason why SizeOf() refuses simple types?
Posted: Sun Nov 09, 2003 6:23 pm
by tinman
As I ask in the subject - is there any *good* reason that SizeOf() refuses to compile when you try to get the size of a simple, built-in type?
Code: Select all
DefType.w foo
Debug SizeOf(foo)
Debug SizeOf(w)
End
Yes, I know that it won't compile. But why have it work like that? Why can't the compiler just stick in the size and be done with it?
Posted: Sun Nov 09, 2003 6:58 pm
by Codemonger
Problem is, how would you deal with this ?
Code: Select all
DefType.w w,foo
Debug SizeOf(foo)
Debug SizeOf(w)
End
Posted: Sun Nov 09, 2003 7:43 pm
by Karbon
Maybe make it SizeOf(.w) to get the size of a type?
Posted: Sun Nov 09, 2003 7:49 pm
by Kale
Code: Select all
DefType.w foo
Debug SizeOf(foo)
Debug SizeOf(w)
End
Of course this will not compile, the var 'w' has not been declared. Remember that in this line:
the '.w' is an assignment NOT a variable.
From the help:
The SizeOf command can be used to find out the size of any complex Structure (it does not work on the simple built-in types such as word and float), Interface or even variables.
Which i guess should be re-edited as now it can get the size variables.
Posted: Mon Nov 10, 2003 12:48 am
by dmoc
I think that, once compiled, there is no type info for simple types. If there was then things would slow down noticeably. Isn't this why Fred provides #LONG, #WORD, etc?
Posted: Mon Nov 10, 2003 12:51 am
by tinman
Kale wrote:Of course this will not compile, the var 'w' has not been declared.
Don't make me hit you with a stick, or something worse.
Code: Select all
Structure blah
a.w
b.w
EndStructure
Debug SizeOf(blah)
End
Types are supported by SizeOf. Why not the built in types? Codemonger has a point with the w variable or w type ambiguity, but other languages manage it. Why can't PB?
Posted: Mon Nov 10, 2003 1:17 am
by Kale
Types are supported by SizeOf.
Only user defined types. Why would you need to know how big built-in types are, you already know?
Posted: Mon Nov 10, 2003 10:04 am
by tinman
Kale wrote:Types are supported by SizeOf.
Only user defined types. Why would you need to know how big built-in types are, you already know?
Because I don't want my code littered with random 1, 2 and 4 values when SizeOf(type) will give it much more meaning. I know there are constants #SIZEOF_B but that's a bit crappy compared to everything being able to be done with SizeOf().
Another example of where this would be useful is building up data section tables of sizes for things - you won't have any variables to use SizeOf() with and again the constant method ain't that nice.
Something else I've just thought of (but can't test here at the mo) - since we have a separate OffsetOf(), can SizeOf tell you the size of a field in a structure? Like:
Code: Select all
Structure blah
a.w
b.w
EndStructure
Debug SizeOf(blah\a)
End
I think that would be a pretty nifty addition too, if it doesn't already do that.
Posted: Mon Nov 10, 2003 4:16 pm
by freak
Ok, for a workaround, you could use SizeOf(WORD) . There is a structure
with only one word inside. Same applys for the other types, too.
Timo
Posted: Mon Nov 10, 2003 8:22 pm
by Num3
Or even easier.... Read the manual
Name Extension Memory consumption Range
Byte .b ->1 byte in memory -128 to +127
Word .w ->2 bytes in memory -32768 to +32767
Long .l -> 4 bytes in memory -2147483648 to +2147483647
Float .f -> 4 bytes in memory unlimited (see below)
String .s -> length of the string + 1 Up to 65536 characters
Posted: Mon Nov 10, 2003 9:45 pm
by Psychophanta
In fact, my sincere opinion: SizeOf() lacks a little bit, because coherence fault.
Moreover, things like Sizeof(blah\a) should work
Posted: Tue Nov 11, 2003 12:49 pm
by Fred
Supporting build-in types isn't a problem. But it raises a problem I have overlooked. What should do the compiler do if a variable is named like a type ? I think than Type should have an higher priority. Sounds ok ?
About the SizeOf(struct\field), it will be implemented.
Posted: Tue Nov 11, 2003 1:47 pm
by freak
> I think than Type should have an higher priority. Sounds ok ?
It allready works like that:
Timo
Posted: Tue Nov 11, 2003 7:09 pm
by Psychophanta
Fred say us:
What should do the compiler do if a variable is named like a type ? I think than Type should have an higher priority. Sounds ok ?
Sounds ok, should be coherent, but i have an idea; what about use this syntax for SizeOf() ?:
Code: Select all
f.f=4.5
debug SizeOf(.f);<-- this if we want know size of floats
debug SizeOf(f);<-- this in case we need to know the size of variable
Posted: Tue Nov 11, 2003 7:36 pm
by Fred
A good idea IHMO. It will break the compatibility with the normal 'C like' SizeOf() so I will see what can be done.