It is currently Wed Jun 19, 2013 2:49 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: A good reason why SizeOf() refuses simple types?
PostPosted: Sun Nov 09, 2003 6:23 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat Apr 26, 2003 4:56 pm
Posts: 1086
Location: Level 5 of Robot Hell
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:
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.10b5)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2003 6:58 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat May 24, 2003 8:02 pm
Posts: 386
Location: Canada
Problem is, how would you deal with this ?

Code:
DefType.w w,foo
 Debug SizeOf(foo)
 Debug SizeOf(w)
End

_________________
<br>"I deliver Justice, not Mercy"

    - Codemonger, 2004 A.D.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2003 7:43 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Mon Jun 02, 2003 1:42 am
Posts: 2013
Location: Ashland, KY
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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 09, 2003 7:49 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3002
Location: Lincoln, UK
Code:
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:
DefType.w foo

the '.w' is an assignment NOT a variable.

From the help:
Quote:
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 12:48 am 
Offline
Enthusiast
Enthusiast

Joined: Sat Apr 26, 2003 12:40 am
Posts: 738
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 12:51 am 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat Apr 26, 2003 4:56 pm
Posts: 1086
Location: Level 5 of Robot Hell
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:
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.10b5)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 1:17 am 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3002
Location: Lincoln, UK
Quote:
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 10:04 am 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat Apr 26, 2003 4:56 pm
Posts: 1086
Location: Level 5 of Robot Hell
Kale wrote:
Quote:
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:
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.10b5)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 4:16 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5184
Location: Germany
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

_________________
Perl – The only language that looks the same before and after RSA encryption.
-- Keith Bostic


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 8:22 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:51 pm
Posts: 2716
Location: Portugal, Lisbon
Or even easier.... Read the manual :P

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

_________________
Software: http://xipa.org
Elevator Maintenance: http://central-elevadores.pt
Good web hosting!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 9:45 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4008
Location: Spa, relaxing and thinking, and thinking...
In fact, my sincere opinion: SizeOf() lacks a little bit, because coherence fault.

Moreover, things like Sizeof(blah\a) should work


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 12:49 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 8951
Location: France
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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 1:47 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5184
Location: Germany
> I think than Type should have an higher priority. Sounds ok ?

It allready works like that:
Code:
POINT.l
Debug SizeOf(POINT)


Timo

_________________
Perl – The only language that looks the same before and after RSA encryption.
-- Keith Bostic


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:09 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Jun 11, 2003 9:33 pm
Posts: 4008
Location: Spa, relaxing and thinking, and thinking...
Fred say us:
Quote:
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:
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



Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 11, 2003 7:36 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 8951
Location: France
A good idea IHMO. It will break the compatibility with the normal 'C like' SizeOf() so I will see what can be done.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye