A good reason why SizeOf() refuses simple types?

Everything else that doesn't fall into one of the other PB categories.
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

A good reason why SizeOf() refuses simple types?

Post 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?
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Codemonger
Enthusiast
Enthusiast
Posts: 384
Joined: Sat May 24, 2003 8:02 pm
Location: Canada
Contact:

Post by Codemonger »

Problem is, how would you deal with this ?

Code: Select all

DefType.w w,foo
 Debug SizeOf(foo) 
 Debug SizeOf(w)
End
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Maybe make it SizeOf(.w) to get the size of a type?
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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:

Code: Select all

DefType.w foo
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.
--Kale

Image
dmoc
Enthusiast
Enthusiast
Posts: 739
Joined: Sat Apr 26, 2003 12:40 am

Post 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?
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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?
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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?
--Kale

Image
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post 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.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
freak
PureBasic Team
PureBasic Team
Posts: 5962
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Or even easier.... Read the manual :P
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
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

In fact, my sincere opinion: SizeOf() lacks a little bit, because coherence fault.

Moreover, things like Sizeof(blah\a) should work
Fred
Administrator
Administrator
Posts: 18553
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5962
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

> I think than Type should have an higher priority. Sounds ok ?

It allready works like that:

Code: Select all

POINT.l
Debug SizeOf(POINT)
Timo
quidquid Latine dictum sit altum videtur
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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() ?: :idea:

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

Fred
Administrator
Administrator
Posts: 18553
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.
Post Reply