list element address question

Just starting out? Need help? Post your questions and find answers here.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

list element address question

Post 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
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: list element address question

Post by idle »

you just inserted an element at 10, so element 30 has become element 31
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: list element address question

Post 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?)
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: list element address question

Post 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
User avatar
Bisonte
Addict
Addict
Posts: 1320
Joined: Tue Oct 09, 2007 2:15 am

Re: list element address question

Post 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...
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
mk-soft
Always Here
Always Here
Posts: 6320
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: list element address question

Post 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
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
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: list element address question

Post 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.
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: list element address question

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