weird linked list behaviour..

Just starting out? Need help? Post your questions and find answers here.
SurreaL
User
User
Posts: 43
Joined: Tue Dec 23, 2003 3:06 am

weird linked list behaviour..

Post 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! :)
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post 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.
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post 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))
Dräc
Enthusiast
Enthusiast
Posts: 150
Joined: Sat Oct 09, 2004 12:10 am
Location: Toulouse (France)
Contact:

Post 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…
SurreaL
User
User
Posts: 43
Joined: Tue Dec 23, 2003 3:06 am

Post 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?
GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

Curiously, on my computer it always returns a value thats 88 away from the desired value. Anyone else?
SurreaL
User
User
Posts: 43
Joined: Tue Dec 23, 2003 3:06 am

Post 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!
SurreaL
User
User
Posts: 43
Joined: Tue Dec 23, 2003 3:06 am

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