Memory List for Strings & Numbers

Share your advanced PureBasic knowledge/code with the community.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Memory List for Strings & Numbers

Post by J. Baker »

Now you can have numerical values and strings in the same list and check its size if needed. And if you allocate memory/string names before your allocated data, you can call on and search your memory data by name. It can be become like a memory Preference. :D

Code: Select all

NewList MemoryList() ;Create a new memory list.

For MemAddress = 0 To 9 ;Save multiple memory addresses in our list.
  AddElement(MemoryList())
  If MemAddress = 3
    MemoryList() = AllocateMemory(80)
    PokeI(MemoryList(), 0123456789)
   Else 
    MemoryList() = AllocateMemory(1)
  EndIf
Next MemAddress
  
SelectElement(MemoryList(), 6) ;Resize and add text to the 7th memory list element.
string.s = "Hello! I'm poking the memory with text."
bytes = StringByteLength(string.s) +2
MemoryList() = ReAllocateMemory(MemoryList(), bytes)
PokeS(MemoryList(), string.s, bytes)

ResetList(MemoryList()) ;Read back the memory addresses and their size.
While NextElement(MemoryList())
  Debug MemoryList()
  Debug MemorySize(MemoryList())
    If ListIndex(MemoryList()) = 3 ;Lets peek at our memory integer.
      Debug PeekI(MemoryList())
    ElseIf ListIndex(MemoryList()) = 6 ;Lets peek at our memory string.
      Debug PeekS(MemoryList())
    EndIf
  FreeMemory(MemoryList()) ;Will free on exit but good practice.
Wend

FreeList(MemoryList()) ;Will free on exit but good practice.
Last edited by J. Baker on Mon Oct 18, 2021 9:57 pm, edited 5 times in total.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
STARGÅTE
Addict
Addict
Posts: 2063
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Memory List for Begginers

Post by STARGÅTE »

Your code gives a "Overflow in a dynamically allocated memory block" in Line 12.
StringByteLength() gives the length of the string without null-character at the end but PokeS() writes an additional null-character.

You have to use either PokeS() with #PB_String_NoZero or allocate 2 more bytes.
Because you use PeekS() without length, you need an additional null-character at the end.

Such memory/string handling is not for "beginners". Beginners should use string lists.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Memory List for Begginers

Post by J. Baker »

Thanks! PB didn't report any errors. Will update it.

If this is not so beginner, should I just remove it?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
STARGÅTE
Addict
Addict
Posts: 2063
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Memory List for Begginers

Post by STARGÅTE »

J. Baker wrote: Mon Oct 18, 2021 8:39 pm Thanks! PB didn't report any errors.
You have to enable the purifier in the compiler settings.
The purifier has a stronger memory overflow detection: Included debugging tools (last item)
J. Baker wrote: Mon Oct 18, 2021 8:39 pm If this is not so beginner, should I just remove it?
I would recommend to change the title to something like "Memory List for strings and numbers" and add an example for using numbers in the same list. How would you decide if a memory block of e.g. 8 bytes is a string or is an integer?
Currently, I can't see your application example "It can be become like a memory Preference".
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Memory List for Strings & Numbers

Post by J. Baker »

Ok, I updated the code and topic title.

Ok, as for the "Preference" idea. I haven't tested it but if you create a memory string "MyValue Integer" before creating a memory with integer(s) in the list, you wouldn't need to remember the ListIndex but could search by name and then get the NextElement(MemoryList()) for the integer information. That may be slower though than just knowing the ListIndex.

Hope that makes sense?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Memory List for Strings & Numbers

Post by wilbert »

For small lists you could also use a structure union of a numeric type with a fixed length string.

Code: Select all

Structure PrefItem
  StructureUnion
    int.q
    str.s{64}
  EndStructureUnion
EndStructure

NewList PrefList.PrefItem()

AddElement(PrefList())
PrefList()\int = 5

AddElement(PrefList())
PrefList()\str = "Hello!"

FirstElement(PrefList())
Debug PrefList()\int
NextElement(PrefList())
Debug PrefList()\str
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Caronte3D
Addict
Addict
Posts: 1014
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Memory List for Strings & Numbers

Post by Caronte3D »

Why StructureUnion? :?
If I remove the StructureUnion the result seems the same.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Memory List for Strings & Numbers

Post by J. Baker »

@wilbert
Thanks! I was thinking of having an unlimited string but even with a StuctureUnion, you could just add another AddElement() if you needed more than 64 characters. So that works too.

@Caronte3D
You don't get an error on line 14, if you remove the StructureUnion? It should say something like, expecting a numerical value instead of a string.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2178
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Memory List for Strings & Numbers

Post by J. Baker »

After more thought, a StructureUnion may make more sense. It's actually shorter in code and for instance, if you were storing strings for a paragraph, your string limit would be the cut off. Plus storing a quad versus integer is easier for both 32 bit and 64 bit compiling. No need to do any checks. Anyway, I was just thinking things through. I may have over-thought it a bit. Thanks for the feedback everyone! ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.
Post Reply