[Implemented] LinkedList and pointers

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

[Implemented] LinkedList and pointers

Post by Dr. Dri »

I just discovered it was possible to do this:

Code: Select all

Structure Bytes
  b.b[0]
EndStructure

Dim *Array.Bytes(3)

*Array(0) = @"A string"
*Array(1) = @"Another string"
*Array(2) = @"some text again"
*Array(3) = @"the end"

For i = 0 To 3
  c = 0
  While *Array(i)\b[c]
    Debug Chr( *Array(i)\b[c] )
    c + 1
  Wend
  Debug "---"
Next i
I tried to do the same with linked lists but it's not possible
(Error: A list name need to start with a character)
It would be very useful to handle pointers lists

Dri
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: LinkedList and pointers

Post by blueznl »

Dr. Dri wrote: Structure Bytes
b.b[0]
EndStructure
why would you like to do this?!?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: LinkedList and pointers

Post by Psychophanta »

Dr. Dri, your code is not safe, because you are making a dynamic array of values.
After asking Fred about safety i made this example to do what you want in a safety way:
viewtopic.php?t=17120

The matter is to allocate enough memory previously to put there the needed amount of bytes. In your example should be:

Code: Select all

Structure Bytes
  b.b[0]
EndStructure

Dim *Array.Bytes(3)

*Array(0)=AllocateMemory(Len("A string")+1)
*Array(1)=AllocateMemory(Len("Another string")+1)
*Array(2)=AllocateMemory(Len("some text again")+1)
*Array(3)=AllocateMemory(Len("the end")+1)
*Array(0) = @"A string"
*Array(1) = @"Another string"
*Array(2) = @"some text again"
*Array(3) = @"the end"

For i = 0 To 3
  c = 0
  While *Array(i)\b[c]
    Debug Chr( *Array(i)\b[c])
    c + 1
  Wend
  Debug "---"
Next
For regular arrays could be:

Code: Select all

Dim *Array.b(3)

*Array(0)=AllocateMemory(Len("A string")+1)
*Array(1)=AllocateMemory(Len("Another string")+1)
*Array(2)=AllocateMemory(Len("some text again")+1)
*Array(3)=AllocateMemory(Len("the end")+1)
*Array(0) = @"A string"
*Array(1) = @"Another string"
*Array(2) = @"some text again"
*Array(3) = @"the end"

For i = 0 To 3
  c = 0
  While PeekB(*Array(i)+c)
    Debug Chr(PeekB(*Array(i)+c))
    c + 1
  Wend
  Debug "---"
Next
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You probably misunderstand something, because the Dr.Dri code seems ok to me. He's affecting a pointer for each element of an array of pointer which is correct. May be i said something wrong in a previous post ? Can you point it to me ?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

So those strings are safe (address won't change) because they are literals? (sorry to barge in, but I thought the @"string" was possibly dangerous).
@}--`--,-- A rose by any other name ..
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

no the adress won't ever change, @"String" isn't dangerous.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Fred wrote:Can you point it to me ?
Yes, but it is deleted :?
viewtopic.php?p=105878

There are a link to it in: viewtopic.php?t=17120
which already exists.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Okay, thanks. :)
@}--`--,-- A rose by any other name ..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Dare2 wrote:So those strings are safe (address won't change) because they are literals? (sorry to barge in, but I thought the @"string" was possibly dangerous).
They are safe if you don't modify them (like this:

Code: Select all

PokeL(@"frank", 89763)
Debug "frank"
)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Fred, yes, sorry, i must correct Dr. Dri code is correct, just because when the strings pointers are defined there is allocated the memory for it automatically, don't?
I mean, a pointer to a string has guarranted an amount of bytes there, which is equal to the size of the string + 1 (the ending null char).

So then, sorry, MR. Dry :oops:

EDITED:
An example for linked list instead array could be like that:

Code: Select all

Structure Bytes
  *b.b[0]
EndStructure

NewList Array.Bytes()
AddElement(Array())
Array()\b = @"A string"
AddElement(Array())
Array()\b = @"Another string"
AddElement(Array())
Array()\b = @"some text again"
AddElement(Array())
Array()\b = @"the end"

ForEach Array()
  c = 0
  While PeekB(Array()\b+c)
    Debug Chr(PeekB(Array()\b+c))
    c + 1
  Wend
  Debug "---"
Next
Last edited by Psychophanta on Tue Jan 31, 2006 4:20 pm, edited 3 times in total.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

It was probably linked to a bug report, that's why it doesn't exists anymore (you can remove the link IMHO).
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

@Fred
Will it be possible to do the same with Linked Lists ?
(that's my question in fact :lol:)

Dri
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

yes
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

Ok thanks :D
maybe we can have more informations ?
already done for V4 ?
will be added for 1st V4 release ?
will be added after V4 release ?

Dri :D
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

It's already done ;).
Post Reply