Page 1 of 1

Memory List for Strings & Numbers

Posted: Mon Oct 18, 2021 7:14 pm
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.

Re: Memory List for Begginers

Posted: Mon Oct 18, 2021 8:12 pm
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.

Re: Memory List for Begginers

Posted: Mon Oct 18, 2021 8:39 pm
by J. Baker
Thanks! PB didn't report any errors. Will update it.

If this is not so beginner, should I just remove it?

Re: Memory List for Begginers

Posted: Mon Oct 18, 2021 8:59 pm
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".

Re: Memory List for Strings & Numbers

Posted: Mon Oct 18, 2021 9:17 pm
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?

Re: Memory List for Strings & Numbers

Posted: Tue Oct 19, 2021 7:51 am
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

Re: Memory List for Strings & Numbers

Posted: Tue Oct 19, 2021 9:57 am
by Caronte3D
Why StructureUnion? :?
If I remove the StructureUnion the result seems the same.

Re: Memory List for Strings & Numbers

Posted: Tue Oct 19, 2021 1:19 pm
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.

Re: Memory List for Strings & Numbers

Posted: Tue Oct 19, 2021 2:01 pm
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! ;)