Page 1 of 1

List element offsets...

Posted: Sun Aug 17, 2003 6:42 pm
by Hi-Toro
OK, I'm not really a PB beginner, but I've never been too great with low-level memory access, pointers, etc, so I thought I'd post here!

I thought that a structure list of long values, like this...

Code: Select all

Structure Example
    entry.l
EndStructure
... would mean each element took 4 bytes, so I imagined each entry in the list could be read via peeking @mylist (), @mylist () + 4, @mylist + 8, etc. However, it seems that each entry here is taking up 24 bytes instead of 4.

Could someone explain why this is? No doubt the answer is really obvious, but it's just not coming to me!

Example code (turn debugger on):

Code: Select all


; Create a structure of long values...

Structure Handles 
    notifier.l 
EndStructure 

; Create a list using this structure...

NewList changes.Handles () 

; Add some elements to it...

For a = 0 To 2
    AddElement (changes ()) 
    changes ()\notifier = Random (1000)
Next

; Print contents...

Debug "Contents of changes () list:"

ResetList (changes ()) 
While NextElement (changes ()) 
    Debug Str (changes ()\notifier) 
Wend 

; Show peeked longs from list...

FirstElement (changes ()) 

Debug ""
Debug "Peeked (wrong results):"
Debug PeekL (@changes ())
Debug PeekL (@changes () + 4)
Debug PeekL (@changes () + 8)

Debug ""
Debug "Peeked (right results):"
Debug PeekL (@changes ())
Debug PeekL (@changes () + 24)
Debug PeekL (@changes () + 48)

Basically, I was hoping to pass @mylist () as the address of an "array of long values" to an OS function (so I can easily add and remove items without having to re-Dim arrays, manipulate banks of memory, etc)...

Re: List element offsets...

Posted: Mon Aug 18, 2003 12:44 am
by Doobrey
Hi-Toro wrote: so I imagined each entry in the list could be read via peeking @mylist (), @mylist () + 4, @mylist + 8, etc. However, it seems that each entry here is taking up 24 bytes instead of 4.
Not quite.. a linked list contains to previous and next elements too.(althought that only makes an extra 8 bytes). Linked lists don`t always reside in one contiguous block of memory either, the mechanism just allocates extra memory when needed and points the "next pointer" of the last element to this new memory.
Doing things this way makes life a lot easier when you want to do things like reorder the elements,as you only have to swap pointers about instead of copying blocks of memory.
Fred did post something in one of the other sections regarding lists a little while ago.

BTW, @mylist() gives you the address of the current element in the list, not the base address of the list.

Ah...

Posted: Mon Aug 18, 2003 12:57 am
by Hi-Toro
Thanks Doobrey -- I completely forgot there would be links to prev/next! I did find a way to work around my previous problem (using lists to easily keep add/remove stuff, counting the list elements, then re-Dimming an array with the contents as needed; a little ugly but it works!)...

As for @mylist () -- yeah, I did eventually figure that part out... ;)