Page 1 of 1

weird linked list behaviour..

Posted: Thu Dec 23, 2004 10:23 pm
by SurreaL
Hello everyone!

Although I purchased PB quite some time ago, I never really looked into it much, which is why I've placed this query in the beginner's forum :) Anyhow I've been playing around with structures, pointers, and linked lists, to try to get a feel for these methods of memory management. I've run into a bit of a hitch however which can be demonstrated with the following code (non relevant structure test stuff removed for brevity):

Code: Select all

;Linked list/pointer exercise
NewList mylist.s()

AddElement (mylist())
mylist () = "element 1"
AddElement (mylist())
mylist () = "element 2"

*ptr = FirstElement (mylist())
Debug mylist()
;Debug PeekS(PeekL(*ptr + 8))
*ptr = NextElement (mylist())
Debug mylist()
;Debug PeekS(PeekL(*ptr + 8))
Ok. If I run the code as-is.. it runs fine. If I uncomment those extra Debug Lines, the first one goes through ok, but the second one crashes the program..! I know that using PeekS and PeekL on the pointer is the 'hard way' of doing this, but once again this was just an exercise to make sure that what made sense in my head would work out in the real-world.

What really confuses me is if I replace NextElement (mylist()) with LastElement (mylist)), the code runs flawlessly! (Even with the weird pointer stuff!) I've asked about this on IRC, and for one person they had said that the code worked either way..? Making me wonder if maybe I'm just on crack :roll:

Anyways.. I've been sure to update to 3.92 using the SmartUpdate program, but perhaps if I see more people claiming this works for them, I will just re-install from scratch!

Any help will be appreciated! :)

Posted: Thu Dec 23, 2004 10:59 pm
by GreenGiant
I have no idea why its not working. I tried using the OnError lib to see and it just said
element 1
An attemped read or write to/from an address to which that process isn't allowed'
But if you were going to use the peek's then you may aswell peek the pointer to the next element rather than using NextElement() to get it like this

Code: Select all

NewList mylist.s()

AddElement (mylist())
mylist () = "element 1"
AddElement (mylist())
mylist () = "element 2"

*ptr = FirstElement (mylist())
Debug PeekS(PeekL(*ptr + 8))
Debug PeekS(PeekL(PeekL(*ptr)+8))
Not an answer as to why its not working I know, but this does seem to work.

Posted: Thu Dec 23, 2004 11:08 pm
by Pupil
It seems like 'NextElement()' doesn't return the same thing that the other commands return!

Code: Select all

;Linked list/pointer exercise
NewList mylist.s()

AddElement (mylist())
mylist () = "element 1"
AddElement (mylist())
mylist () = "element 2"

*ptr = FirstElement (mylist())
Debug mylist()
Debug PeekS(PeekL(*ptr + 8))
Debug *ptr+8
!MOV eax, dword [e_mylist]
!ADD eax, 8
!MOV dword [p_ptr], eax
Debug *ptr
*ptr = NextElement (mylist())
Debug mylist()
Debug *ptr+8
!MOV eax, dword [e_mylist]
!ADD eax, 8
!MOV dword [p_ptr], eax
Debug *ptr
Debug PeekS(PeekL(*ptr))

Posted: Thu Dec 23, 2004 11:40 pm
by Dräc
I confirme Pupil observation:
The crash takes place on the result returned by NextElement() function.
Unfortunately, NextElement() doesn’t return the address of the expected structure.
It’s apparently a bug…

Posted: Thu Dec 23, 2004 11:43 pm
by SurreaL
Ok another simplified example to demonstrate the problem..it indeed appears as though NextElement is the culprit! (How such a bug could go unnoticed is beyond me.. but again I'm willing to accept I've done something wrong!)

Code: Select all

;Linked list/pointer exercise
NewList mylist.s()

AddElement (mylist())
mylist () = "element 1"
AddElement (mylist())
mylist () = "element 2"

*ptr = FirstElement (mylist())
*ptr2 = NextElement (mylist())
*ptr3 = LastElement (mylist())

Debug *ptr
Debug *ptr2
Debug *ptr3
Of course NextElement and LastElement *should* point to the same area in memory, but they don't..? What's going on?

Posted: Thu Dec 23, 2004 11:48 pm
by GreenGiant
Curiously, on my computer it always returns a value thats 88 away from the desired value. Anyone else?

Posted: Thu Dec 23, 2004 11:54 pm
by SurreaL
Same for me, GreenGiant.. It's again pretty weird to see this considering one would assume that NextElement would simply follow the structure for the linked list element (as defined in the linked list help docs), but this does not seem to be the case, as the following code exhibits:

Code: Select all

;Linked list/pointer exercise
NewList mylist.s()

AddElement (mylist())
mylist () = "element 1"
AddElement (mylist())
mylist () = "element 2"

*ptr = FirstElement (mylist())
*ptr2 = NextElement (mylist())
*ptr3 = LastElement (mylist())

Debug "First Element: " + Str(*ptr)
Debug "Next Element: " + Str(*ptr2)
Debug "Last Element: " + Str(*ptr3)
Debug "First Element's pointer to Next Element: " + Str(PeekL(*ptr))
Looks like, with all the similar errroneous results you guys are getting.. I should post this in the bug reports forum!

Posted: Fri Dec 24, 2004 12:06 am
by SurreaL
ok the post is up! Sorry for cross-posting! I originally thought this must have been something I was personally doing wrong in the source, but now it appears that that may not be so :/

Anyhow, I suppose I could always write my own "NextElement" function in the meanwhile..