Author Topic: SizeOf  (Read 2028 times)

0 Members and 1 Guest are viewing this topic.

  • Guest
SizeOf
« on: September 28, 2018, 07:45:28 AM »
Code: [Select]
DIM z AS ZSTRING * 260
print SizeOf(z)
' Output: 1

DIM ws AS WSTRING * 260
print SizeOf(ws)
' Output: 4

How is that? I expected 260 and 520.

Charles Pegge

  • Guest
Re: SizeOf
« Reply #1 on: September 28, 2018, 08:11:17 AM »
sizeof refers to the size of each element, in these cases, 1 or 2
countof is the element count
bytesof is the number of bytes occupied by the array.

In this case, null terminator chars are included in the count

Code: [Select]
wchar w="hello"
print sizeof w
print countof w
print bytesof w

The other string types are pointers, and therefore have a size of sys, but you can still get the character size using widthof

Mike Lobanovsky

  • Guest
Re: SizeOf
« Reply #2 on: September 28, 2018, 10:04:36 AM »
[troll mode on]

heightof, breadthof, and depthof are reserved for future use.

[troll mode off]

:D

  • Guest
Re: SizeOf
« Reply #3 on: September 28, 2018, 10:28:21 AM »
So many xxxxxof and none does what I want, that is to return the capacityof.

In other dialects, when used with null terminated strings, sizeof returns its maximum length, not counting the terminating nuill(s). It is often used when calling API functions in which you have to pass the length of the buffer, instead of passing an hard coded number.

« Last Edit: September 28, 2018, 10:35:27 AM by José Roca »

Charles Pegge

  • Guest
Re: SizeOf
« Reply #4 on: September 28, 2018, 12:56:22 PM »
This will deliver exactly the number of bytes you ask for:

Code: [Select]
wchar w[50]
print sizeof w '2
print countof w '50
print bytesof w '100

We have a few more ofs
typeof
typecodeof
encodingof
structureof
recordof

useful for diagnostics and introspection.

José Roca

  • Guest
Re: SizeOf
« Reply #5 on: September 28, 2018, 08:18:56 PM »
Thanks, Charles. I was using WSTRING * 260 because in FreeBasic WSTRING is a null terminated unicode string, like your ASCIIZ2, not a variable-length unicode string like in O2 or PowerBasic, which begs the question: Is WSTRING * 260 valid in O2 or it is incorrect but the compiler ignores the mistake?

Charles Pegge

  • Guest
Re: SizeOf
« Reply #6 on: September 28, 2018, 08:41:43 PM »
I see the compiler ignores it!

Aurel

  • Guest
Re: SizeOf
« Reply #7 on: September 29, 2018, 03:00:22 AM »
If you ask me it is better that ignore because wstring * 260 looks to me unlogical  ::)
variable type as wide-string multiply with 260 ..fb is strange.
however is this ok

'size/len/count/width
[b]string s
s = "Oxygen"
print "sizeOf-"  & sizeOf s  ' res 4 b
print "Len-"     & Len s     ' res 6 c
print "countof-" & countOf s ' res 1
print "widthOf-" & widthOf s ' res 1[/b]

José Roca

  • Guest
Re: SizeOf
« Reply #8 on: September 29, 2018, 03:50:17 AM »
I used WSTRING by mistake because in FreeBasic, that I use everyday, it is not a variable-length string, but the quivalent to O2's ASCIIZ2. What I expect in such a case is an error from the compiler.

Charles Pegge

  • Guest
Re: SizeOf
« Reply #9 on: September 29, 2018, 05:29:30 AM »
I'll trap this syntax for all types. It is inconsistent with the language.

It conflicts with:
dim as asciiz *s
'char *s
'
« Last Edit: September 29, 2018, 05:43:42 AM by Charles Pegge »

Charles Pegge

  • Guest
Re: SizeOf
« Reply #10 on: September 29, 2018, 09:02:16 AM »
typecodeof could be very useful when converting variants.

This is a list of primitives and their typecodes (second number), as used by the compiler:

Code: [Select]
void      15 0x00 '* Used by reference only
sbyte     15 0x01
byte      15 0x21
ubyte     15 0x21
string    15 0xe1
string2   15 0xe2
wstring   15 0xe2
bstring   15 0xc1
gstr_     15 0xc1 '* Accept any Bstring width. for core functions only
bstr      15 0xc1
bstring2  15 0xc2
char      15 0xa1
wchar     15 0xa2
cstr_     15 0xa1 '* Accept any char width. for core functions only
asciiz    15 0xa1
zstring   15 0xa1
zstring2  15 0xa2
asciiz2   15 0xa2
short     15 0x02
long      15 0x04
wide      15 0xa2
int       15 0x04
integer   15 0x04
float     15 0x64
single    15 0x64
double    15 0x68
extended  15 0x6A
quad      15 0x48 '* using FPU on 32 bit systems (was 08), returns data on edx:eax
word      15 0x22
dword     15 0x24
ulong     15 0x24
uint      15 0x24
any       15 0x08 '* to be used by reference only
sys       15 0x08 '* 4/8 depending on 32/64 bit system
bool      15 0x04 '* MS
boolean   15 0x01 '* MS
fpu       15 0x40 '* fast call using fpu registers (not exportable)
signed    15 0x04
unsigned  15 0x24
qword     15 0x28 '* Assembler only

JRS

  • Guest
Re: SizeOf
« Reply #11 on: September 29, 2018, 09:50:04 AM »
Quote
typecodeof could be very useful when converting variants.

VARIANTS - Music to my ears!

Arnold

  • Guest
Re: SizeOf
« Reply #12 on: September 30, 2018, 10:39:42 AM »
The description of these keywords is missing in the Oxygen Helpfile:

countof
bytesof
widthof

Does spanof have the same meaning as countof?

I also found that using Indexbase 0 makes a difference for some keywords. (the default is indexbase 1)