POINTER NULL CHECK

General FreeBASIC programming questions.
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: POINTER NULL CHECK

Postby jj2007 » Nov 26, 2017 21:15

fxm wrote: 'Len (string)' is neither true nor false, but conversely 'Len (string) = 0' is true or false.

I suspect it's just a habit. In one of my older sources I find 200 instances of IF LEN(some$), only two have IF LEN(some$)>0.
One is

Code: Select all

IF LEN(a$)>0 AND LEN(b$)>0 THEN ...
Actually, omitting the >0 might pose a problem here (and only here) if the Basic dialect treats AND bitwise, e.g. with len(a$)=2 but len(b$)=1:

Code: Select all

IF LEN(a$) AND LEN(b$) THEN ...

where (2 and 1) is false.
dodicat
Posts: 5990
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: POINTER NULL CHECK

Postby dodicat » Nov 26, 2017 21:39

You've lost me here.
true or false are boolean.
len(s) and len(s)>0 are both integers.
Do I miss something?
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: POINTER NULL CHECK

Postby Munair » Nov 26, 2017 21:52

dodicat wrote:You've lost me here.
true or false are boolean.
len(s) and len(s)>0 are both integers.
Do I miss something?

IF tests are boolean by definition. They are either TRUE or FALSE, regardless of the values being tested. Strictly speaking, checking IF LEN(string) THEN returns TRUE if the length is anything but NULL. Checking IF LEN(string) > 0 THEN returns TRUE only if the length is greater than NULL. The latter is the correct way to test since string lengths can never be negative.
dodicat
Posts: 5990
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: POINTER NULL CHECK

Postby dodicat » Nov 26, 2017 22:06

-1 and 0 are the two possible returns.

If tests are Boolean (Capital B)
I think they were part of C.Bools thesis.

boolean datatypes are a new freebasic addition.
true/false ... 1/0
Thus my mix up.
fxm
Posts: 9258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: POINTER NULL CHECK

Postby fxm » Nov 26, 2017 22:07

In my previous post, true and false are not the intrinsic Boolean constants but only qualifiers for the tested condition (verified and not verified).
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: POINTER NULL CHECK

Postby Munair » Nov 26, 2017 22:20

dodicat wrote:-1 and 0 are the two possible returns.

If tests are Boolean (Capital B)
I think they were part of C.Bools thesis.

boolean datatypes are a new freebasic addition.
true/false ... 1/0
Thus my mix up.

TRUE and FALSE are outcomes while data type Boolean is merely a convient way to hold either of them. Remember QuickBASIC where you could define

Code: Select all

TRUE = -1
FALSE = NOT TRUE

Yet, QuickBASIC did not have a Boolean data type.

Since the beginning of the computer age, it was generally accepted that TRUE is anything non-zero, while FALSE is always zero. So testing IF LEN(string) THEN returns TRUE if the outcome is non-zero. It is up to a compiler to translate that to -1 or anyting other than zero. ;)
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: POINTER NULL CHECK

Postby jj2007 » Nov 26, 2017 23:09

Munair wrote:So testing IF LEN(string) THEN returns TRUE if the outcome is non-zero. It is up to a compiler to translate that to -1 or anyting other than zero. ;)

As the disassembly above shows, the compiler doesn't bother to translate that to a boolean or integer value; it translates to

Code: Select all

jz skipthis   ; jump if zero for the if len() then case
jle skipthis  ; jump if less or equal for the if len()>0 then case
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: POINTER NULL CHECK

Postby Munair » Nov 27, 2017 0:35

jj2007 wrote:As the disassembly above shows, the compiler doesn't bother to translate that to a boolean or integer value; it translates to

Code: Select all

jz skipthis   ; jump if zero for the if len() then case
jle skipthis  ; jump if less or equal for the if len()>0 then case

It does when a return value from the evaluation is requested. Here, the ASM instructions test for exactly the opposite. So if you test for LEN(string) (<>0) then JZ jumps if not true. Likewise, when testing for LEN(string) > 0 JLE jumps if not true.

But:

Code: Select all

print 1 > 2 ' output: 0
print 1 > 0 ' output -1
return value requested.
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: POINTER NULL CHECK

Postby jj2007 » Nov 27, 2017 10:04

Munair wrote:
jj2007 wrote:As the disassembly above shows, the compiler doesn't bother to translate that to a boolean or integer value

It does when a return value from the evaluation is requested.

That is correct (avoiding to write 'true'), but what does it return: An integer or a boolean? Take a little test...

Code: Select all

Dim result as boolean

Print 1<2
Print 1>2

result=(1<2)
print "1<2=", result
result=(1>2)
print "1>2=", result

print true
print false
fxm
Posts: 9258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: POINTER NULL CHECK

Postby fxm » Nov 27, 2017 10:10

It always returns an Integer:

Code: Select all

#print typeof(1<2)
Compiler output wrote:INTEGER
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: POINTER NULL CHECK

Postby Munair » Nov 27, 2017 10:28

It looks like the compiler does an evaluation before producing ASM code. In case of PRINT (1 < 2) the value -1 is returned immediately:

Code: Select all

  mov   edx, 1
  mov   rsi, -1
  mov   edi, 0
  call   fb_PrintLongint@PLT

When trying to evaluate IF TRUE = FALSE THEN it seems clear to the compiler that no operation (NOP) is necessary.
jj2007
Posts: 1259
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: POINTER NULL CHECK

Postby jj2007 » Nov 27, 2017 12:37

fxm wrote:It always returns an Integer

Yep! Funny, isn't it? Not true or false but rather a number...
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: POINTER NULL CHECK

Postby Munair » Nov 27, 2017 12:50

jj2007 wrote:
fxm wrote:It always returns an Integer

Yep! Funny, isn't it? Not true or false but rather a number...

Computers are very stupid. They only recognize on or off: 10011011000101 :)
fxm
Posts: 9258
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: POINTER NULL CHECK

Postby fxm » Nov 27, 2017 13:01

fxm wrote:It always returns an Integer
except all the same if all the terms of the condition are already Boolean.
D.J.Peters
Posts: 7838
Joined: May 28, 2005 3:28

Re: POINTER NULL CHECK

Postby D.J.Peters » Nov 27, 2017 18:39

Code: Select all

#define CBOOLEAN(expression) iif((expression),1,0)
var BASIC  =         (1>2) shl 1 or         (1=2) shl 2 or         (1<2) shl 3
var C_Code = CBOOLEAN(1>2) shl 1 or CBOOLEAN(1=2) shl 2 or CBOOLEAN(1<2) shl 3
print BASIC,C_CODE
sleep

Return to “General”

Who is online

Users browsing this forum: Majestic-12 [Bot], MSN [Bot] and 1 guest