It is currently Mon Sep 23, 2019 6:47 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: ChangeCurrentElement()
PostPosted: Sat Aug 03, 2019 11:39 am 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3655
Location: Berlin, Germany
In the third-last line of the following code, ChangeCurrentElement() changes to an element that does not exist anymore!
In the next line, Debug shows the content of that not-existing element!

Fred, when you are working on ChangeCurrentElement() anyway for fixing this bug, it probably won't be much additional work to implement this related wish. :-)

Code:
; PB 5.71 beta 2 (x64) on Windows 10

EnableExplicit

Define *p
NewList lst.i()

AddElement(lst()) : lst() = 12
AddElement(lst()) : lst() = 15
*p = @ lst()
Debug @ lst()                      ; -> 7998400 in my test
Debug lst()                        ; -> 15 (as expected)

DeleteElement(lst())
Debug ListSize(lst())              ; -> 1  (as expected)
Debug @ lst()                      ; -> 7998368 in my test
Debug lst()                        ; -> 12 (as expected)

ChangeCurrentElement(lst(), *p)
Debug @ lst()                      ; -> 7998400 in my test (How is that possible???)
Debug lst()                        ; -> 15                 (How is that possible???)

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: ChangeCurrentElement()
PostPosted: Sat Aug 03, 2019 12:44 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5780
Location: Germany
The documentation states:
Quote:
The element must be a pointer to another element which exists in this list.


You called the function with invalid input, hence the behavior is undefined.

No bug.

_________________
quidquid Latine dictum sit altum videtur


Top
 Profile  
Reply with quote  
 Post subject: Re: ChangeCurrentElement()
PostPosted: Sat Aug 03, 2019 12:46 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sat May 05, 2007 5:31 pm
Posts: 609
Location: Linz, Austria
Code:
; PB 5.71 beta 2 (x64) on Windows 10

EnableExplicit

Define *p
NewList lst.i()

AddElement(lst()) : lst() = 12
AddElement(lst()) : lst() = 15
*p = @ lst()
Debug @ lst()                      ; -> 7998400 in my test
Debug lst()                        ; -> 15 (as expected)

DeleteElement(lst())
Debug ListSize(lst())              ; -> 1  (as expected)
Debug @ lst()                      ; -> 7998368 in my test
Debug lst()                        ; -> 12 (as expected)
*p = #Null

ChangeCurrentElement(lst(), *p)
Debug @ lst()                      ; -> 7998400 in my test (How is that possible???)
Debug lst()                        ; -> 15                 (How is that possible???)


Top
 Profile  
Reply with quote  
 Post subject: Re: ChangeCurrentElement()
PostPosted: Sat Aug 03, 2019 2:13 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 1214
Location: Germany, Glienicke
@Little John:

For speed optimization DeleteElement() doesn't delete the memory of the element or its content,
it just changes the links for next and previous element.
For speed optimization ChangeCurrentElement() doesn't check the pointer whether it is valid.

Thats a nice feature of Pure Basic linked List and not a bug.

_________________
ImageImage


Top
 Profile  
Reply with quote  
 Post subject: Re: ChangeCurrentElement()
PostPosted: Sat Aug 03, 2019 2:27 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 1915
Location: Germany
Make sure the element is still there.
Code:
;-TOP

; List Managment by mk-soft

EnableExplicit

Structure udtMyList
  refCount.i
  iVal.i
EndStructure

Global MutexMyList = CreateMutex()

Procedure GetListElement(List MyList.udtMyList())
  LockMutex(MutexMyList)
  MyList()\refCount + 1
  UnlockMutex(MutexMyList)
  ProcedureReturn @MyList()
EndProcedure

Procedure FreeListElement(List Mylist.udtMyList(), *ppMyList.Integer, Delete = #True)
  Protected *pMyList.udtMyList
  LockMutex(MutexMyList)
  If *ppMyList\i
    *pMyList.udtMyList = *ppMyList\i
    If *pMyList\refCount > 0
      *pMyList\refCount - 1
      If *pMyList\refCount = 0 And Delete = #True
        If @Mylist() = *pMyList
          DeleteElement(Mylist())
        Else
          PushListPosition(MyList())
          ChangeCurrentElement(Mylist(), *pMyList)
          DeleteElement(Mylist())
          PopListPosition(Mylist())
        EndIf
      EndIf
    EndIf
    *ppMyList\i = 0
  EndIf
  UnlockMutex(MutexMyList)
EndProcedure

Procedure DeleteListElement(List MyList.udtMyList())
  LockMutex(MutexMyList)
  If MyList()\refCount = 0
    DeleteElement(MyList())
  EndIf
  UnlockMutex(MutexMyList)
EndProcedure

Global NewList MyData.udtMyList()

AddElement(MyData())
MyData()\iVal = 1
AddElement(MyData())
MyData()\iVal = 2
AddElement(MyData())
MyData()\iVal = 3
AddElement(MyData())
MyData()\iVal = 4


SelectElement(MyData(), 2)
Debug "Get Pointer A"
Define *a.udtMyList = GetListElement(MyData())
Debug "Get Pointer B"
Define *b.udtMyList = GetListElement(MyData())
Debug "Get Pointer C"
SelectElement(MyData(), 1)
Define *c.udtMyList = GetListElement(MyData())

ForEach MyData()
  Debug "refCount = " + MyData()\refCount + " | Value = " + MyData()\iVal
Next

Debug "Free Pointer A"
FreeListElement(MyData(), @*a)
ForEach MyData()
  Debug "refCount = " + MyData()\refCount + " | Value = " + MyData()\iVal
Next

Debug "Free Pointer B"
FreeListElement(MyData(), @*b)
ForEach MyData()
  Debug "refCount = " + MyData()\refCount + " | Value = " + MyData()\iVal
Next

Debug "Free all unused ListElements"
ForEach MyData()
  DeleteListElement(MyData())
Next
ForEach MyData()
  Debug "refCount = " + MyData()\refCount + " | Value = " + MyData()\iVal
Next

Debug "Free Pointer C"
FreeListElement(MyData(), @*c)
ForEach MyData()
  Debug "refCount = " + MyData()\refCount + " | Value = " + MyData()\iVal
Next

_________________
My Projects ThreadToGUI / OOP-BaseClass / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: ChangeCurrentElement()
PostPosted: Sat Aug 03, 2019 2:33 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3655
Location: Berlin, Germany
@STARGÅTE:

Yes, I see now.
I previouly believed that PureBasic would keep internally an inventory of pointers which a valid for a particular list. As freak explained this is not the case. So I do understand why ChangeCurrentElement() behaves the way it currently does.

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye