Page 1 of 1

list element address question

Posted: Sat May 18, 2024 4:03 am
by jassing
This is probably a question for Fred...
Can this be expected (address of elements staying the same)
(for me, the address stays the same...)

Code: Select all

Structure tset
  fld1.s
  fld2.i
EndStructure

NewList test.tset()

For x = 1 To 100 Step 3
  AddElement(tesT()) : test()\fld2=x
  i=Random(20,5) ; make the string random in length, to make things as random as possible
  For z =1 To i
    test()\fld1+Chr( Random(90,65))
  Next
Next

SelectElement(test(),30)
Debug test()\fld2
Debug Hex(@test())

SelectElement(test(),10)

InsertElement(test()) : test()\fld2=999
test()\fld1="Just me"

SelectElement(test(),31)
Debug test()\fld2
Debug Hex(@test())

SelectElement(test(),10)
test()\fld2=9999
test()\fld1="This is not me!"

SelectElement(test(),31)
Debug test()\fld2
Debug Hex(@test())

ShowVariableViewer()
CallDebugger

Re: list element address question

Posted: Sat May 18, 2024 4:47 am
by idle
you just inserted an element at 10, so element 30 has become element 31

Re: list element address question

Posted: Sat May 18, 2024 5:50 am
by jassing
idle wrote: Sat May 18, 2024 4:47 am you just inserted an element at 10, so element 30 has become element 31
Yes, but the address of the element didn't change...

so element #30 has an address of X
Now insert something before 30, 30 becomes 31
element #31 has an address of X
Change data before 31, and...
element #31 has an address of X

The element,remaining the same, doesn't change memory address.
(NB if I delete an element, then the address changes;but I'm really not even interested in inserting...just adding elements.
Will the address of a given element remain or is there some housekeeping that occurs at some interval that would change the elements address?)

Re: list element address question

Posted: Sat May 18, 2024 7:17 am
by idle
No it won't a list is heap allocated nodes with a pointer to the next element. So the address of the element won't change but it's position will if you insert or delete an element

Re: list element address question

Posted: Sat May 18, 2024 8:40 am
by Bisonte
jassing wrote: Sat May 18, 2024 5:50 am...but I'm really not even interested in inserting...just adding elements....
But beware... you are not adding only at the end of the list :

Code: Select all

NewList a()

For i= 1 To 10
  AddElement(a())
  a() = i
Next i

SelectElement(a(), 3)
AddElement(a())
a() = 200

ForEach a()
  Debug a()
Next
You see that it is also insert items...

Re: list element address question

Posted: Sat May 18, 2024 10:12 am
by mk-soft
The memory address of the element does not change. Only the pointers to the next and previous element

PB-SDK Update v1.02

Code: Select all

;-TOP by mk-soft, v1.02, 18.05.2024

Structure sdkListHeader
  *Next.sdkListHeader
  *Prev.sdkListHeader
EndStructure

Procedure _LagElementPtr(*List)
  Protected *Element.sdkListHeader
  If *List
    *Element = *List - SizeOf(sdkListHeader)
    If *Element\Prev
      ProcedureReturn *Element\Prev + SizeOf(sdkListHeader)
    EndIf
  EndIf
  ProcedureReturn 0
EndProcedure

Procedure _LeadElementPtr(*List)
  Protected *Element.sdkListHeader
  If *List
    *Element = *List - SizeOf(sdkListHeader)
    If *Element\Next
      ProcedureReturn *Element\Next + SizeOf(sdkListHeader)
    EndIf
  EndIf
  ProcedureReturn 0
EndProcedure

Macro LagElementPtr(_List_)
  _LagElementPtr(@_List_)
EndMacro

Macro LeadElementPtr(_List_)
  _LeadElementPtr(@_List_)
EndMacro

; ****

Structure udtData
  iVal.i
  sVal.s
EndStructure

Global NewList MyData.udtData()

For i = 0 To 9
  AddElement(MyData())
  MyData()\iVal = i
Next

Debug "Output"
ForEach MyData()
  Debug "Value: " + MyData()\iVal
  Debug "Addr Lag: " + LagElementPtr(MyData())
  Debug "Addr Act: " + @MyData()
  Debug "Addr Lead: " + LeadElementPtr(MyData())
Next

SelectElement(MyData(), 5)
AddElement(MyData())
MyData()\iVal = 1111

Debug "Output 2"
ForEach MyData()
  Debug "Value: " + MyData()\iVal
  Debug "Addr Lag: " + LagElementPtr(MyData())
  Debug "Addr Act: " + @MyData()
  Debug "Addr Lead: " + LeadElementPtr(MyData())
Next

SelectElement(MyData(), 6)
a = MyData()\iVal
*LagElement.udtData = LagElementPtr(MyData())
If *LagElement
  a + *LagElement\iVal
EndIf
*LeadElement.udtData = LeadElementPtr(MyData())
If *LeadElement
  a + *LeadElement\iVal
EndIf
Debug "Sum: " + a

Re: list element address question

Posted: Sat May 18, 2024 2:25 pm
by jassing
idle wrote: Sat May 18, 2024 7:17 am No it won't a list is heap allocated nodes with a pointer to the next element. So the address of the element won't change but it's position will if you insert or delete an element
That's fine -- that's what I wanted to know. thanks Idle.

Re: list element address question

Posted: Sat May 18, 2024 2:26 pm
by jassing
mk-soft wrote: Sat May 18, 2024 10:12 am The memory address of the element does not change. Only the pointers to the next and previous element

PB-SDK Update v1.02
thank you.