Oxygen Basic
Programming => Problems & Solutions => Topic started by: on September 28, 2018, 07:45:28 AM
-
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.
-
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
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
-
[troll mode on]
heightof, breadthof, and depthof are reserved for future use.
[troll mode off]
:D
-
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.
-
This will deliver exactly the number of bytes you ask for:
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.
-
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?
-
I see the compiler ignores it!
-
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]
-
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.
-
I'll trap this syntax for all types. It is inconsistent with the language.
It conflicts with:
dim as asciiz *s
'char *s
'
-
typecodeof could be very useful when converting variants.
This is a list of primitives and their typecodes (second number), as used by the compiler:
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
-
typecodeof could be very useful when converting variants.
VARIANTS - Music to my ears!
-
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)