Checking if linked list exists
Checking if linked list exists
As lists are passed to a procedure by reference, what is the best way to detect if a list exists from the procedure?
Re: Checking if linked list exists
Without a working code example ...
I had to look in my crystal ball again.
Maybe you mean something like that:
I had to look in my crystal ball again.
Maybe you mean something like that:
Code: Select all
Structure TestStructure
List TestList.s()
EndStructure
Procedure Test(*Test.TestStructure)
If ListSize(*Test\TestList())
Debug "Ok"
EndIf
EndProcedure
Define Test.TestStructure
;AddElement(Test\TestList())
Test(@Test)
Re: Checking if linked list exists
Hi infratec,
I am not sure about using ListSize() to determine if a list exists. If a list does not exist, the app crashes in IDE when you use ListSize(). Interestingly there isn't an IsList() PB function.
I am not sure about using ListSize() to determine if a list exists. If a list does not exist, the app crashes in IDE when you use ListSize(). Interestingly there isn't an IsList() PB function.
Code: Select all
NewList MyList.s()
; FreeList(MyList()) ; uncomment this line and app crashes in IDE
If ListSize(MyList()) >= 0
MessageRequester("", "List exists")
Else
MessageRequester("", "List does not exist")
EndIf
Last edited by Tawbie on Sun Mar 27, 2022 3:59 am, edited 6 times in total.
Re: Checking if linked list exists
See Help manual TYPEOF()
Re: Checking if linked list exists
AFAIK , TypeOf() can only be used to find out the type of a variable or a structure field, not if a list exists. For example:
Code: Select all
Structure Test
List Names.s()
EndStructure
If TypeOf(Test\Names) = #PB_List
Debug "Names is a list"
EndIf
Re: Checking if linked list exists
Why...should a linked list be used as a parameter, but does not exist? Usually the compiler spits out an error (when using EnableExplicit, which I always recommend) and otherwise it would be a...let's say stupid error. I can't think of a situation where this is needed.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: Checking if linked list exists
The manual says that FreeList(MyList()) return nothing
In fact it return 1 if it is executed
Tested w PB 5.7? x86 - Windows 11 x64
In fact it return 1 if it is executed
Tested w PB 5.7? x86 - Windows 11 x64
Code: Select all
NewList MyList.s()
mylist = FreeList(MyList()) ; uncomment this line and the app crashes
If mylist = 1
Debug "no such list"
Else
Debug "List exist"
EndIf
Code: Select all
Macro _FreeList(var)
If FreeList(var)
freeflag = 1
Else
freeflag = 0
EndIf
EndMacro
NewList MyList.s()
_FreeList(MyList()) ; uncomment this line and the app crashes
If freeflag = 0
Debug "OK"
Else
Debug "No such List"
EndIf
Egypt my love
Re: Checking if linked list exists
@RASHAD
not work on macOS and Linux
We missing function IsList(list)
Macro IsList(r1, list)
not work on macOS and Linux
We missing function IsList(list)
Macro IsList(r1, list)
Code: Select all
;-TOP *** IsList ***
CompilerIf Not Defined(PB_Compiler_Backend, #PB_Constant)
#PB_Compiler_Backend = 0
#PB_Backend_Asm = 0
#PB_Backend_C = 1
CompilerEndIf
CompilerSelect #PB_Compiler_Backend
CompilerCase #PB_Backend_Asm
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
Macro IsList(_r1, _list)
!mov eax, [t_#_list]
!mov [v_#_r1], eax
EndMacro
CompilerElse
Macro IsList(_r1, _list)
!mov rax, [t_#_list]
!mov [v_#_r1], rax
EndMacro
CompilerEndIf
CompilerCase #PB_Backend_C
Macro IsList(_r1, _list)
!v_#_r1=(void*)(t_#_list.a);
EndMacro
CompilerEndSelect
; ****
NewList MyList.i()
Define r1
CompilerIf #PB_Compiler_Backend = #PB_Backend_Asm
; ASM Case sensitive
e = ListSize(MyList())
IsList(r1, MyList)
Debug r1
FreeList(MyList())
IsList(r1, MyList)
Debug r1
CompilerElse
; C-Backend low case
e = ListSize(MyList())
IsList(r1, mylist)
Debug r1
FreeList(mylist())
IsList(r1, mylist)
Debug r1
CompilerEndIf
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Checking if linked list exists
@jacdelad and @tawbie: That was the conclusion I came to; let the compiler catch the error if the list doesn't exist. I'm new to PureBasic so I was just fishing to see what method other people used. In our organization, we share procedures, functions, libraries among ourselves quite a bit so when I create one I try to make mine as bullet proof as possible. I'm sorry I don't have a code example because the question didn't pertain to just one piece of code.
@mk-soft: I haven't mastered all the compiler directives in PB yet but I am going to study your example. Thank you.
@mk-soft: I haven't mastered all the compiler directives in PB yet but I am going to study your example. Thank you.
Re: Checking if linked list exists
@Catdaddy: I must revert my last post, I didn't think of FreeList(), because I've never used it. But I still think it's better to avoid any situation with unknown list type or status.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
-
- Enthusiast
- Posts: 109
- Joined: Sat Sep 21, 2019 4:24 pm
Re: Checking if linked list exists
ClearList could be a solution, clear all elements from the list releasing their memory but the list is empty and ListSize can be used.
You can check my games at:
https://ricardo-sdl.itch.io/
https://ricardo-sdl.itch.io/
Re: Checking if linked list exists
Depends on whether you're programming for yourself or for others. When programming for others it could make sense to check whether a list exists or not, but the programmer of the calling program should make sure to only make valid calls.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Re: Checking if linked list exists
I posted a workaround for C and ASM backend (x64 and x86) on another thread:
viewtopic.php?p=602012#p602012
viewtopic.php?p=602012#p602012
- Distorted Pixel
- Enthusiast
- Posts: 233
- Joined: Sun Aug 29, 2021 4:34 am
Re: Checking if linked list exists
It has been awhile for posts in this thread, but I happen to search for a way to detect if a list exists and found this thread. I worked on something for a bit and came up with the following. It doen't detect if it isn't there, but if it is and there is no elements in it, it returns a "-1" So in a way if it returns a "-1" then you know the list exists with no elements.
If you add an element it will return "0". If you add a second element it will return a "1" and so on.
I tried to use SelectElement() and if there isn't any elements the program errors and says the list is not initialized, so I went with the above code
If you add an element it will return "0". If you add a second element it will return a "1" and so on.
Code: Select all
NewList MyList.s()
index=ListIndex(mylist())
Debug index
To be popular is way to much work. I just want to be me, myself and I. Oh no, does that mean I'm bipolar?
No one cares how much you know until they know how much you care
No one cares how much you know until they know how much you care
Re: Checking if linked list exists
I...think I don't understand what you want to achieve.
In your example, the list has to exist. SelectElement() creates an error or crashes if it can't be performed, but I would use ListSize() beforehand to prevent that.
ListIndex() just tells us where the pointer is set to the list (the active element). If the list is empty, it is -1, sure (no element selected). If you add elements, it can still become -1 again (by using ResetList()). So this is not a universal way to tell if a list is empty, nor that it exists.
In your example, the list has to exist. SelectElement() creates an error or crashes if it can't be performed, but I would use ListSize() beforehand to prevent that.
ListIndex() just tells us where the pointer is set to the list (the active element). If the list is empty, it is -1, sure (no element selected). If you add elements, it can still become -1 again (by using ResetList()). So this is not a universal way to tell if a list is empty, nor that it exists.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB