Page 1 of 2

[Implemented] LinkedList and pointers

Posted: Tue Jan 31, 2006 11:15 am
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

Re: LinkedList and pointers

Posted: Tue Jan 31, 2006 11:18 am
by blueznl
Dr. Dri wrote: Structure Bytes
b.b[0]
EndStructure
why would you like to do this?!?

Re: LinkedList and pointers

Posted: Tue Jan 31, 2006 1:43 pm
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

Posted: Tue Jan 31, 2006 2:39 pm
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 ?

Posted: Tue Jan 31, 2006 2:41 pm
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).

Posted: Tue Jan 31, 2006 2:46 pm
by Fred
no the adress won't ever change, @"String" isn't dangerous.

Posted: Tue Jan 31, 2006 2:50 pm
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.

Posted: Tue Jan 31, 2006 2:52 pm
by Dare2
Okay, thanks. :)

Posted: Tue Jan 31, 2006 2:54 pm
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"
)

Posted: Tue Jan 31, 2006 3:00 pm
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

Posted: Tue Jan 31, 2006 3:14 pm
by Fred
It was probably linked to a bug report, that's why it doesn't exists anymore (you can remove the link IMHO).

Posted: Tue Jan 31, 2006 5:35 pm
by Dr. Dri
@Fred
Will it be possible to do the same with Linked Lists ?
(that's my question in fact :lol:)

Dri

Posted: Tue Jan 31, 2006 5:44 pm
by Fred
yes

Posted: Tue Jan 31, 2006 5:50 pm
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

Posted: Tue Jan 31, 2006 6:05 pm
by Fred
It's already done ;).