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.
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.
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.
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".
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.
@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.
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!