Don't understand Lists and...
BlitzMax Forums/BlitzMax Beginners Area/Don't understand Lists and...
| ||
A standard situation: You define a user Type and create a List to manage the data content. But what are the relationships between the List and the Type variable furtherly ? My first idea was: Furtherly you have two divided data contents, one in the List and one in the Type variable, each going its own way. But if you change the Type variable after adding it to the List, the List data also is changed. If you on the other hand change the Lists content, the Type Variables content is also changed. So are they two representation of a single data block somewhere in the RAM ? Two instances of a hidden third object ? But if you then set the Type variable to NULL, the Lists content isn't influenced at all (in other words: it's not ALSO setted to Null). Same rules for the Type variable if you delete the Lists entry. How comes that ? Does anyone know, what concepts lie under this behaviour ? Strict Graphics 800, 600,0 SetClsColor 0,0,0'133, 133 ,133 ' a Type.... Type test Field ID:String ="" Field a:Int End Type '...and a List to hold the data of the Type Global allTests:TList =CreateList() ' make one Type variable and add it to the List ' to investigate furtherly their relationships Local tmpTest:test =New Test tmpTest.ID ="First" tmpTest.a =111 ListAddLast allTests, tmpTest DebugLog "-------------------------------------" DebugLog "the Types variable content:" DebugLog tmpTest.ID DebugLog tmpTest.a DebugLog "" DebugLog "the Lists data content:" For Local curTest:test =EachIn allTests DebugLog curTest.ID DebugLog curTest.a Next DebugLog "-------------------------------------" DebugLog "set Types a to 555:" tmpTest.a =555 DebugLog tmpTest.a DebugLog "the effect for the Lists data content : " For Local curTest:test =EachIn allTests DebugLog curTest.ID DebugLog curTest.a Next DebugLog "-------------------------------------" DebugLog "Reversed way: set the Lists data content to 777 :" For Local curTest:test =EachIn allTests curTest.a =777 DebugLog curTest.ID DebugLog curTest.a Next DebugLog "the effect for the Type variable: " DebugLog tmpTest.a 'Rem DebugLog "-------------------------------------" DebugLog "Now delete the Type variable..." tmpTest =Null ' this now would give an error message "Null object" : 'DebugLog tmpTest.a DebugLog "What happens to the List (nothing !) : " For Local curTest:test =EachIn allTests DebugLog curTest.ID DebugLog curTest.a Next DebugLog "-------------------------------------" 'EndRem Rem ' alternatively.... DebugLog "-------------------------------------" DebugLog "Now delete the List content..." For Local curTest:test =EachIn allTests ListRemove allTests, curTest Next allTests =Null DebugLog "What happens to the Type variable (nothing !) : " DebugLog tmpTest.a DebugLog "-------------------------------------" EndRem |
| ||
You have 2 pointers (the list entry and the variable) to a single object. If you remove the object from the list and the variable goes out of scope the memory is released next GC run. You *have* to remove the object from the list and, in your example, the variable never goes out of scope. If you lose all pointers to the object (i.e. remove from list and reuse tmptest or tmptest=nul or a local variable goes out of scopel) you can no longer reference the object and GC will remove the object. |
| ||
Thanks, that makes it clearer to me. |
| ||
[quote]But what are the relationships between the List and the Type variable furtherly ?[quote]There isn't one. The list is a data structure, the type is structured data, that "describes" an object. A list can hold objects. |