Checking if linked list exists

Just starting out? Need help? Post your questions and find answers here.
Catdaddy
User
User
Posts: 12
Joined: Wed Mar 23, 2022 3:40 pm

Checking if linked list exists

Post 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?
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Checking if linked list exists

Post 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)
Tawbie
User
User
Posts: 26
Joined: Fri Jul 10, 2020 2:36 am

Re: Checking if linked list exists

Post 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
Last edited by Tawbie on Sun Mar 27, 2022 3:59 am, edited 6 times in total.
hrcoder
User
User
Posts: 10
Joined: Sat Apr 01, 2017 1:54 am
Location: USA

Re: Checking if linked list exists

Post by hrcoder »

See Help manual TYPEOF()
Tawbie
User
User
Posts: 26
Joined: Fri Jul 10, 2020 2:36 am

Re: Checking if linked list exists

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Checking if linked list exists

Post 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.
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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Checking if linked list exists

Post 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

Egypt my love
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Checking if linked list exists

Post 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
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
Catdaddy
User
User
Posts: 12
Joined: Wed Mar 23, 2022 3:40 pm

Re: Checking if linked list exists

Post 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.
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Checking if linked list exists

Post 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.
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
ricardo_sdl
Enthusiast
Enthusiast
Posts: 109
Joined: Sat Sep 21, 2019 4:24 pm

Re: Checking if linked list exists

Post 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.
You can check my games at:
https://ricardo-sdl.itch.io/
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Checking if linked list exists

Post 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.
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
User avatar
Regenduft
Enthusiast
Enthusiast
Posts: 121
Joined: Mon Mar 02, 2009 9:20 pm
Location: Germany

Re: Checking if linked list exists

Post by Regenduft »

I posted a workaround for C and ASM backend (x64 and x86) on another thread:
viewtopic.php?p=602012#p602012
User avatar
Distorted Pixel
Enthusiast
Enthusiast
Posts: 233
Joined: Sun Aug 29, 2021 4:34 am

Re: Checking if linked list exists

Post 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
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? :shock:

No one cares how much you know until they know how much you care
User avatar
jacdelad
Addict
Addict
Posts: 1431
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Checking if linked list exists

Post 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.
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
Post Reply