An #Empty$ and #Null$ chat.

Everything else that doesn't fall into one of the other PB categories.
Amilcar Matos
User
User
Posts: 43
Joined: Thu Nov 27, 2014 3:10 pm
Location: San Juan, Puerto Rico

An #Empty$ and #Null$ chat.

Post by Amilcar Matos »

An #Empty$ and #Null$ chat.

Beware – Non-English speakers be aware that this short story plays with word meanings. Since PureBasic community is a multi-language community every effort has been made to simplify the story so that it be understood equally well by non english speakers. This is fiction. Enjoy!
Amílcar Matos Pérez – author
San Juan, Puerto Rico
(c)2016 AMP All rights reserved. Free to copy and translate with authorship acknowledgment.

Introduction – Deep inside the PB 5.40 compiler live a long and complex string of bits, a new electronic life form. Thanks to the Help file we get to know them.
#Empty$ - Hi! #Null$.
#Null$ - (in a sad voice) Hi. #Empty$.
#Empty$ - How things are going?
#Null$ - Well, i’m depressed.
#Empty$ - Why? Look... the computer is off; RAM is empty nothing to bother us.
#Null$ - Yeesss..., but nothing is unemployed. You and I found work with PB 5.40 compiler, but nothing was left out.
#Empty$ - How come is that? We all are equal, right?
#Null$ - Wrong, my friend. We are not equal. Coders believe we are equal but we are not. We are not constant at all!
Test code:

Code: Select all

UserMessage$ = "Both constants are different."
If #Empty$ = #Null$
  UserMessage$ = "Both constants are equal."
EndIf
MessageRequester ("Info", UserMessage$, #mb_iconexclamation)
End
#Null$ - You see we do exactly the same work, but we are not equal.
#Empty$ - But, but; I’m always empty, i do a constant job.
#Null$ - Sure and me too.
Test code:

Code: Select all

VarOne$ = #Empty$
VarTwo$ = #Null$
UserMessage$ = "Both variables are different."
If VarOne$ = VarTwo$
  UserMessage$ = "Both variables are equal."
EndIf
MessageRequester ("Info", UserMessage$, #mb_iconexclamation)
End
#Empty$ - Shh! Here comes the compiler commander Mr. EnableExplicit.
EnableExplicit - Hello guys! How are things going?
(Silence, full of #Empty$ comments and #Null$ expressions.)
EnableExplicit - Very well. Everything seems in order. Bye, guys.
#Null$ - See, our work makes the compiler happy.
Test code:

Code: Select all

EnableExplicit

Define VarOne$
Define VarTwo$
Define UserMessage$
Define UserMessage1$
Define UserMessage2$
Define UserMessage3$
Define UserMessage4$

; this code block illustrates the instructions to free a variable completely.
; VarOne$ is kept alive as an empty variable.
VarOne$ = #Empty$
; VarTwo$ is freed completely.
VarTwo$ = #Null$

; Show content of both variables.
; VarOne$ works as it is expected.
Debug "Empty VarOne = "   + VarOne$              
Debug "VarOne content = " + Asc(VarOne$)  
Debug "VarOne length = "  + Len(VarOne$)  
Debug ""

; The next statement should not work since enable explicit is active and
; the variable has already been [completely freed!].
; "freed" is understood as erased, deleted, non existent, undefined.
; with enable explicit active a compilation error is expected.
; but no compilation error is raised.
Debug "Null VarTwo = " + VarTwo$              
Debug "VarTwo content = " + Asc(VarTwo$)  
Debug "VarTwo length = "  + Len(VarTwo$)  
Debug ""

UserMessage1$ = "Both variables are different."
If VarOne$ = VarTwo$
  UserMessage1$ = "Both variables are equal."
EndIf

UserMessage2$ = "VarTwo with null value is different to empty constant."
If VarTwo$ = #Empty$
  UserMessage2$ = "VarTwo with null value is equal to empty constant."
EndIf

UserMessage3$ = "VarOne with empty value is different to null constant."
If VarOne$ = #Null$
  UserMessage3$ = "VarOne with empty value is equal to null constant."
EndIf

UserMessage4$ = "Both constants are different."
If #Empty$ = #Null$
  UserMessage4$ = "Both constants are equal."
EndIf

UserMessage$ = UserMessage1$ + #LF$ +
               UserMessage2$ + #LF$ +
               UserMessage3$ + #LF$ +
               UserMessage4$ + #LF$
MessageRequester ("Info", UserMessage$, #mb_iconexclamation)

End
#Empty$ - But, how come this can be? We do the same job and we are different. We are suppossed to be constants – always the same. Please, don’t let that depress you.
#Null$ - Oh, no, no! I’m not depressed because of that.
#Empty$ - Then, why?
#Null$ - Because the compiler left nothing out.
#Empty$ - I don’t understand you, i feel myself blank.
#Null$ - I expected nothing... become a constant like you and me.
#Empty$ - Look i know nothing. Nothing can delete variables, procedures even modules. The Team just output nothing from the code section.
#Null$ - Exactly! And nothing now lives happy in the comment section been himself and where nothing it do.
#Empty$ - Don’t worry he is happy been himself and doing nothing.
#Null$ - Maybe, anyway...; I’m a constant and i’m suppossed to free a variable completely (help file says so). Once i do my job the variable should be deleted, vanished, non-existent; for that i need nothing. Since variables still exists after i do my job that means nothing was left out of work.
So, my work is exactly the same as yours and you know what will happen in a future update..., i will be deleted.
I just don’t know what i will then do, as i need the job!
#Empty$ - Now don’t be so sad. You know how PureBasic coders are. They truly are quality coders, they will learn to like you and they need you to document their API accesses. So they will not let the Team unemploy you. Cheer up friend.
Come on read History page (16th October 2015 : Version 5.40) There it says;
Added: #Null$ special string constant to affect a real null value to a string (To free it completely). It can be used as well for API functions which sometimes accept NULL instead of a string address.
#Null$ - #True says the same and i hope they’ll retain me. It’s just #False who says that the Team will delete you instead of me.
#Empty$ - Delete me! Hmmm...; HA!... ha, ha!!
#Null$ - What are you laughing at?
#Empty$ - It would be meaningless, and pointless to delete me. How are they going to tell the coders of the world in their next update?
Deleted - #Empty$.
See the entry is empty, instead of me, nothing was deleted.

The End. :D

Reference – http://www.purebasic.fr/english/viewtop ... t=%23Empty

On the serious side, this story also plays with the different perspectives on the way constants should work and be understood, no offense intended. Please understand this lightly as my purpose is to amuse, not to critique or offend in any way whatsoever.
Admin, please, if anyone feels hurt or offended by this story feel free to erase it completely. Thank you.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: An #Empty$ and #Null$ chat.

Post by kenmo »

Good story! But I think you misunderstand some things...
#Empty$ and #Null$ have different purposes.

#Empty$ is the same as using "" (points to a place in allocated memory which holds just a NULL character).
#Null$ sets a string variable to true 0 (a null pointer, does not point to any place in memory).

#Null$ does not remove a variable from existence, just resets its value, so EnableExplicit won't complain.

Here are some examples:

Code: Select all

; Run with PB 5.41

Define VarOne$
Debug "VarOne$ begins as a null pointer"
Debug "Points to: " + Str(@VarOne$)
Debug ""

VarOne$ = ""
Debug "VarOne$ is now a pointer to an empty string (one null character)"
Debug "Points to: " + Str(@VarOne$)
Debug "Char there: " + Str(PeekC(@VarOne$))
Debug ""

VarOne$ = "A"
Debug "VarOne$ is now a pointer to a string (an 'A' then a null terminator)"
Debug "Points to: " + Str(@VarOne$)
Debug "Char there: " + Str(PeekC(@VarOne$))
Debug ""

VarOne$ = #Empty$
Debug "Using #Empty$ is the same as using " + #DQUOTE$ + #DQUOTE$
Debug "Points to: " + Str(@VarOne$)
Debug "Char there: " + Str(PeekC(@VarOne$))
Debug ""

VarOne$ = #Null$
Debug "Using #Null$ is different (sets it back to a null pointer)"
Debug "Points to: " + Str(@VarOne$)
Debug "(The variable VarOne$ still exists, just points nowhere)"
Debug ""

VarTwo$ = #Empty$
Debug "PB compares a null string and an empty string as equal"
Debug "@VarOne$: " + Str(@VarOne$)
Debug "@VarTwo$: " + Str(@VarTwo$)
Debug "Char at @VarTwo$: " + Str(PeekC(@VarTwo$))
Debug "Equal? " + Str(Bool(VarOne$ = VarTwo$))
Debug ""

Debug "The constants #Null$ and #Empty$ do NOT compare as equal"
Debug "(maybe they should?)"
Debug "but that comparison should never appear in any real program."
Debug "Equal? " + Str(Bool(#Null$ = #Empty$))
Debug ""
Amilcar Matos
User
User
Posts: 43
Joined: Thu Nov 27, 2014 3:10 pm
Location: San Juan, Puerto Rico

Re: An #Empty$ and #Null$ chat.

Post by Amilcar Matos »

Thank you, kenmo, for setting the record straight.
Thank you very much.
Dudemeister
User
User
Posts: 29
Joined: Sat Oct 26, 2019 6:48 pm

Re: An #Empty$ and #Null$ chat.

Post by Dudemeister »

Very interesting conversation!

Regarding kenmo's statement -
#Null$ does not remove a variable from existence, just resets its value, so EnableExplicit won't complain.
Just out of pure curiosity, does PB have a keyword or method to remove variables from existence?
box_80
Enthusiast
Enthusiast
Posts: 111
Joined: Mon Sep 03, 2012 8:52 pm

Re: An #Empty$ and #Null$ chat.

Post by box_80 »

Dudemeister wrote: Sun May 23, 2021 8:13 pm Very interesting conversation!

Regarding kenmo's statement -
#Null$ does not remove a variable from existence, just resets its value, so EnableExplicit won't complain.
Just out of pure curiosity, does PB have a keyword or method to remove variables from existence?
Not that I know of. If you what to be able to remove a variables from existence. You would have to create a variables in a data structure like a map. Might be other ways, just not as easy.
Dudemeister
User
User
Posts: 29
Joined: Sat Oct 26, 2019 6:48 pm

Re: An #Empty$ and #Null$ chat.

Post by Dudemeister »

@box_80

Mostly, it was just a curiosity. But PB is beginning to become as addictive for me as coffee. I would at some point like to probe the minds of the PB masters to learn the proper way to dispose of/clean up each type of variable and objects. It's one of my pet peaves. Even in languages which use garbage collection I destroy my referenceable variables and objects (both managed and unmanaged). I'm often told it's a waste of code and time, but I do it anyway... it lets me sleep well at night knowing I have prevented potential memory leaks.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: An #Empty$ and #Null$ chat.

Post by mk-soft »

There are only a few rules to follow

Variables: Variables that are created as Global, Define are valid for the entire runtime and do not have to be deleted at runtime.

Variables inside procedures that are defined as Protect are cleaned up when the procedure is exited.

Pointers and structures!
Memory areas defined with AllocateMemory must be deleted with FreeMemory. Contents, if any, that show string, object or other memory must be released beforehand.

If memory is created with AllocateStructure, FreeStructure is released again.
Standard variable types (String, List, Map, Array) are automatically cleaned up by FreeStructure. References to objects must be released beforehand.


PB Object Management (window, gadgets, images, etc)

A distinction is made between objects with constants (internally an array of objects) and objects created with #PB_Any.
Objects created with #PB_Any Create[Object] MUST be released again with Free[Object]. Overwriting is not possible.
Objects created with constants can be overwritten. The previous object is automatically deleted.

At the end of the programme, all objects are automatically released.

However, since a new thread would make sense here
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Dudemeister
User
User
Posts: 29
Joined: Sat Oct 26, 2019 6:48 pm

Re: An #Empty$ and #Null$ chat.

Post by Dudemeister »

@mk-soft

This is some thorough and useful knowledge to have. And you are correct... I should have started a new thread with my question.

I greatly appreciate the information!.!.!
Post Reply