Page 1 of 2
Checking if linked list exists
Posted: Sat Mar 26, 2022 10:34 pm
by Catdaddy
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
Posted: Sat Mar 26, 2022 11:39 pm
by infratec
Without a working code example ...
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
Posted: Sun Mar 27, 2022 2:36 am
by Tawbie
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.
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
Re: Checking if linked list exists
Posted: Sun Mar 27, 2022 3:12 am
by hrcoder
See Help manual TYPEOF()
Re: Checking if linked list exists
Posted: Sun Mar 27, 2022 3:36 am
by Tawbie
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
Posted: Sun Mar 27, 2022 3:50 am
by jacdelad
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.
Re: Checking if linked list exists
Posted: Sun Mar 27, 2022 4:07 am
by RASHAD
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
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
Re: Checking if linked list exists
Posted: Sun Mar 27, 2022 2:02 pm
by mk-soft
@RASHAD
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
Re: Checking if linked list exists
Posted: Sun Mar 27, 2022 7:43 pm
by Catdaddy
@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.
Re: Checking if linked list exists
Posted: Mon Mar 28, 2022 3:50 am
by jacdelad
@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.
Re: Checking if linked list exists
Posted: Mon Mar 28, 2022 1:14 pm
by ricardo_sdl
ClearList could be a solution, clear all elements from the list releasing their memory but the list is empty and ListSize can be used.
Re: Checking if linked list exists
Posted: Mon Mar 28, 2022 5:38 pm
by jacdelad
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.
Re: Checking if linked list exists
Posted: Tue May 30, 2023 12:05 pm
by Regenduft
I posted a workaround for C and ASM backend (x64 and x86) on another thread:
viewtopic.php?p=602012#p602012
Re: Checking if linked list exists
Posted: Wed Nov 22, 2023 1:09 am
by Distorted Pixel
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.
Code: Select all
NewList MyList.s()
index=ListIndex(mylist())
Debug index
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
Re: Checking if linked list exists
Posted: Wed Nov 22, 2023 1:48 am
by jacdelad
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.