Code: Select all
dim handle as any ptr
if handle = NULL then ... ' OK
if not handle then ... ' error: wrong type argument to bit-complement
Code: Select all
dim handle as any ptr
if handle = NULL then ... ' OK
if not handle then ... ' error: wrong type argument to bit-complement
Code: Select all
dim handle as any ptr
if handle = 0 then Print "NULL"
if not handle then Print "Handle="; handle
handle=1
if handle = 0 then Print "NULL"
if not handle then Print "Handle="; handle
Sleep
Code: Select all
NULL
Handle=0
Handle=1
Code: Select all
if not handle then Print "not handle=";not handle
Code: Select all
dim handle as any ptr
if handle = NULL then ... ' handle = 0
if handle then ... ' handle <> 0
Code: Select all
dim handle as any ptr
if handle = NULL then ... ' handle = 0
if handle <> NULL then ... ' handle <> 0
No, it isn't, use: 0 (instead of NULL), since NULL might NOT be, defined!Munair wrote:So is the first test HANDLE = NULL effective?
I defined it as 0 in my library. ;)MrSwiss wrote:No, it isn't, use: 0 (instead of NULL), since NULL might NOT be, defined!Munair wrote:So is the first test HANDLE = NULL effective?
(it isn't "native", aka: default defined, by compiler)
I noticed. Thanks.fxm wrote:When compiling with gcc, complement a pointer is disallowed (works with gas).
gcc is stricter (or gas more lax)
Workaround:orCode: Select all
dim handle as any ptr if handle = NULL then ... ' handle = 0 if handle then ... ' handle <> 0
Code: Select all
dim handle as any ptr if handle = NULL then ... ' handle = 0 if handle <> NULL then ... ' handle <> 0
The problem is maybe that some programmers don't pay much attention to the consequences:Munair wrote:fxm wrote:When compiling with gcc, complement a pointer is disallowed (works with gas).
gcc is stricter (or gas more lax)
Code: Select all
Dim handleI as integer
Dim handleB as boolean
handleI=123
handleB=123
if not handleI then
print "not a handle: ";not handleI
else
print "valid handle: "; handleI
endif
if not handleB then
print "not a handle: ";not handleB
else
print "valid handle: "; handleB
endif
Sleep
Code: Select all
not a handle: -124
valid handle: true
Code: Select all
dim handle as any ptr
if handle then ... ' works
if not handle then ... 'doesn't work
Code: Select all
dim handle as any ptr
dim k as integer
If ( handle = k ) Then ... ' OK
If ( handle <> k ) Then ... ' OK
If Not ( handle = k ) Then ... ' OK
If Not ( handle <> k ) Then ... ' OK
Code: Select all
If Len(string) Then ...
In QuickBASIC, testing with LEN produced faster code than with quotes:fxm wrote:Another example, I don't like the syntax:Code: Select all
If Len(string) Then ...
Code: Select all
if len(MyString) then... ' faster
if MyString <> "" then ... 'slower
Code: Select all
if len(MyString) = 0 then... ' faster
if MyString = "" then ... ' slower
I thought rather:fxm wrote:Another example, I don't like the syntax:Code: Select all
If Len(string) Then ...
Code: Select all
If Len(string) > 0 Then ...
Code: Select all
#define pnot(x) -(cast(integer,x)+1)
print "integers"
for n as long=1 to 20
var x=int(rnd*100-rnd*100)
print x,not x,pnot(x)
next
print
redim as long a(1 to 200001)
print "Pointers"
dim as any ptr p
for n as long=1 to 20
p=@a(1+rnd*20000)
if n=10 then p=0
print p,pnot(p)
next
print
print "test 2000000 doubles"
for n as long=1 to 2000000
var x=(rnd*1000000-rnd*1000000)
if not(x)<>pnot(x) then print x,"error"
next
print "done"
sleep
I agree.fxm wrote:I thought rather:(not omitting "> 0")Code: Select all
If Len(string) > 0 Then ...
fxm wrote:Another example, I don't like the syntax:Code: Select all
If Len(string) Then ...
Code: Select all
Dim MyString as string="123"
asm int 3
if len(MyString) Then
print "ifLen"
endif
asm int 3
if len(MyString)>0 Then
print "ifLenGtzero"
endif
Code: Select all
Address Hex dump Command Comments
0040161B ³. CC int3
0040161C ³. 6A FF push -1 ; ÚArg2 = -1
0040161E ³. 8D45 E8 lea eax, [ebp-18] ; ³
00401621 ³. 50 push eax ; ³Arg1 => offset LOCAL.6
00401622 ³. E8 49120000 call 00402870 ; ÀTmpFile.00402870
00401627 ³. 85C0 test eax, eax
00401629 ³. 74 0E jz short 00401639
0040162B ³. 6A 01 push 1 ; ÚArg3 = 1
0040162D ³. 68 40804000 push offset 00408040 ; ³Arg2 = UNICODE "ifLen"
00401632 ³. 6A 00 push 0 ; ³Arg1 = 0
00401634 ³. E8 C7080000 call 00401F00 ; ÀTmpFile.00401F00
00401639 ³> CC int3
0040163A ³. 6A FF push -1 ; ÚArg2 = -1
0040163C ³. 8D45 E8 lea eax, [ebp-18] ; ³
0040163F ³. 50 push eax ; ³Arg1 => offset LOCAL.6
00401640 ³. E8 2B120000 call 00402870 ; ÀTmpFile.00402870
00401645 ³. 85C0 test eax, eax
00401647 ³. 7E 0E jle short 00401657
00401649 ³. 6A 01 push 1 ; ÚArg3 = 1
0040164B ³. 68 4C804000 push offset 0040804C ; ³Arg2 = UNICODE "ifLenGtzero"
00401650 ³. 6A 00 push 0 ; ³Arg1 = 0
00401652 ³. E8 A9080000 call 00401F00 ; ÀTmpFile.00401F00
Obviously this syntax compiles well and works but this code BASIC is not "clean" in my humble opinion, because the expression 'Len (string)' is neither true nor false, but conversely the expression 'Len (string) = 0' is true or false.fxm wrote:Another example, I don't like the syntax:Code: Select all
If Len(string) Then ...