Run throug a list of strings by memory address only

Just starting out? Need help? Post your questions and find answers here.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Run throug a list of strings by memory address only

Post by Lebostein »

Can I run throug a list of strings if I know the memory address only?

Code: Select all

Structure test
  int1.i
  int2.i
  int3.i
  int4.i
  int5.i
  List text.s()
  int6.i
  int7.i
  int8.i
EndStructure

Define test.test
AddElement(test\text())
test\text() = "str1"
AddElement(test\text())
test\text() = "str2"
AddElement(test\text())
test\text() = "str3"

*address = @test + OffsetOf(test\text)
User avatar
StarBootics
Addict
Addict
Posts: 984
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Run throug a list of strings by memory address only

Post by StarBootics »

Probably not simply because your structure "test" contain only the list header and not all the element the list might have.

Best regards
StarBootics.
The Stone Age did not end due to a shortage of stones !
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Run throug a list of strings by memory address only

Post by Lebostein »

hm.... I thought in the list header you can see whether a next element (by adress) exists or not...
User avatar
idle
Always Here
Always Here
Posts: 5093
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Run throug a list of strings by memory address only

Post by idle »

It's not an array so no you can't get at it directly. What do you you want to do?
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Run throug a list of strings by memory address only

Post by Lebostein »

I have no idea what is happening here, but with a little reverse engineering + trial and error I get his:

Code: Select all

Procedure.s PeekX(*memory): Protected *address, string.s

  *address = PeekI(*memory)
  If *address: string = PeekS(*address): EndIf
  ProcedureReturn string

EndProcedure

Structure test
  int1.i
  int2.i
  int3.i
  int4.i
  int5.i
  List text.s()
  int6.i
  int7.i
  int8.i
EndStructure

Define test.test
AddElement(test\text())
test\text() = "str1"
AddElement(test\text())
test\text() = "str2"
AddElement(test\text())
test\text() = "str3"

*address = @test + OffsetOf(test\text)

*address = PeekI(*address + 8)
Debug PeekX(*address + 16) ; ---> "str3"

*address = PeekI(*address + 8)
Debug PeekX(*address + 16) ; ---> "str2"

*address = PeekI(*address + 8)
Debug PeekX(*address + 16) ; ---> "str1"

*address = PeekI(*address + 8)
Debug *address ; ---> *address returns ZERO, seems the end of the list
What exactly am I doing here? And why I scroll backwards to the list?
User avatar
idle
Always Here
Always Here
Posts: 5093
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Run throug a list of strings by memory address only

Post by idle »

You've picked up the previous pointer, but what's wrong with using a foreach? I'm just trying to understand what your motivation is are you serialising data or something like that.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Run throug a list of strings by memory address only

Post by Lebostein »

idle wrote: Mon Apr 26, 2021 7:46 am You've picked up the previous pointer, but what's wrong with using a foreach? I'm just trying to understand what your motivation is are you serialising data or something like that.
I am writing an universal procedure to write the content of a structured variable to a string or file, like InsertJSONStructure(). But not as JSON or XML but as a Python dictionairy. To get it universal, it must be based on the address alone, just like InsertJSONStructure()....
User avatar
idle
Always Here
Always Here
Posts: 5093
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Run throug a list of strings by memory address only

Post by idle »

Lebostein wrote: Mon Apr 26, 2021 7:57 am
idle wrote: Mon Apr 26, 2021 7:46 am You've picked up the previous pointer, but what's wrong with using a foreach? I'm just trying to understand what your motivation is are you serialising data or something like that.
I am writing an universal procedure to write the content of a structured variable to a string or file, like InsertJSONStructure(). But not as JSON or XML but as a Python dictionairy
Ok thanks.
That's tricky to do for an arbitrary structure at runtime as we don't have the required reflection information to serialize the structures, I have tried in the past and it's messy but you can do it with JSON, so maybe it's easier to do that and then convert it to Pythons dictionary format which is a Trie.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: Run throug a list of strings by memory address only

Post by Sicro »

Lebostein wrote: Mon Apr 26, 2021 4:42 amCan I run throug a list of strings if I know the memory address only?

Code: Select all

Structure test
  int1.i
  int2.i
  int3.i
  int4.i
  int5.i
  List text.s()
  int6.i
  int7.i
  int8.i
EndStructure

Define test.test
AddElement(test\text())
test\text() = "str1"
AddElement(test\text())
test\text() = "str2"
AddElement(test\text())
test\text() = "str3"

Structure StringListStruc
  List s.s()
EndStructure

*address.StringListStruc = @test + OffsetOf(test\text)
ForEach *address\s()
  Debug *address\s()
Next
Lebostein wrote: Mon Apr 26, 2021 7:57 amI am writing an universal procedure to write the content of a structured variable to a string or file, like InsertJSONStructure().
InsertJSONStructure is a special function that we cannot build ourselves. The PB compiler creates a data section for this command with the variable names and so on, because normally neither structures nor variable names exist at the end in the EXE file.

There is a keyword Runtime which can be used to make the PB compiler store variable names etc. in the EXE, but this does not work with structures.
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
skywalk
Addict
Addict
Posts: 3996
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Run throug a list of strings by memory address only

Post by skywalk »

Code like this confuses me since this is the very reason databases exist.
SQL supports querying a database schema and tables.
SQL supports reading/writing a database's contents.
If you want to reproduce the programming generics of a database, you will fall short and most likely fail.
If you want to hardcode some specific structures, then you might have a slight speed advantage over SQL, but you still fail in that any structure changes force more code changes.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Olli
Addict
Addict
Posts: 1071
Joined: Wed May 27, 2020 12:26 pm

Re: Run throug a list of strings by memory address only

Post by Olli »

Lebostein wrote: Mon Apr 26, 2021 7:29 am I have no idea what is happening here, but with a little reverse engineering + trial and error I get his:

Code: Select all

Procedure.s PeekX(*memory): Protected *address, string.s

  *address = PeekI(*memory)
  If *address: string = PeekS(*address): EndIf
  ProcedureReturn string

EndProcedure

Structure test
  int1.i
  int2.i
  int3.i
  int4.i
  int5.i
  List text.s()
  int6.i
  int7.i
  int8.i
EndStructure

Define test.test
AddElement(test\text())
test\text() = "str1"
AddElement(test\text())
test\text() = "str2"
AddElement(test\text())
test\text() = "str3"

*address = @test + OffsetOf(test\text)

*address = PeekI(*address + 8)
Debug PeekX(*address + 16) ; ---> "str3"

*address = PeekI(*address + 8)
Debug PeekX(*address + 16) ; ---> "str2"

*address = PeekI(*address + 8)
Debug PeekX(*address + 16) ; ---> "str1"

*address = PeekI(*address + 8)
Debug *address ; ---> *address returns ZERO, seems the end of the list
What exactly am I doing here? And why I scroll backwards to the list?
:D
Very good. I have the same feeling when I am drunk and I just have forcomen to open the door of my car with a hanger after having lock up the car keys inside the passenger compartment !

What about if you insert a list element pointer change ?
-->

Code: Select all

...
Define test.test
AddElement(test\text())
test\text() = "str1"
AddElement(test\text())
test\text() = "str2"
AddElement(test\text())
test\text() = "str3"
FirstElement(test\text() )
...
Post Reply