Page 1 of 1

confusion about memory allocation with structures and dynamic elements

Posted: Thu Nov 14, 2024 3:13 pm
by emperor-limitless
hello
I am a newbie in pure basic, and well, a newbie in low leveled programming, which involves memory management, there's one confusion I'm having when I have a structure that contains a dynamic field, such as a string, or a list/map/etc,my question is, do I just use

Code: Select all

*MyPtr = allocateMemory(sizeOf(MyStruct))
and then when deallocating I do

Code: Select all

FreeMemory(*MyPtr)
or do I do

Code: Select all

*MyPtr = AllocateStructure(MyStruct)
then

Code: Select all

FreeStructure(*MyPtr)
OR I just don't need to allocate unless in specific cases? My, lacking knowledge of low leveled programming tells me that allocating everything to the stack might explode at my face :lol:, and when are the cases when I want to use initialize structure and not? I'm a little overwhelmed here :scream:

Re: confusion about memory allocation with structures and dynamic elements

Posted: Thu Nov 14, 2024 4:53 pm
by SMaag
For Structures use
*MyPtr = AllocateStructure(MyStruct)

With AllocateMemory you will run into a problem if you have dynamic structures.

see the PB Help
Only with AllocateStructure you get a correct intialized List()
AllocateMemory will only reserve space for the Pointer to List but do not initialize the List

Code: Select all

Structure People
    Name$
    List Friends$()
  EndStructure
  
  *DynamicPeople.People = AllocateStructure(People)
  *DynamicPeople\Name$ = "Fred"
  AddElement(*DynamicPeople\Friends$())
  *DynamicPeople\Friends$() = "Stef"
  
  Debug *DynamicPeople\Name$
  Debug *DynamicPeople\Friends$()
  
  FreeStructure(*DynamicPeople)


Re: confusion about memory allocation with structures and dynamic elements

Posted: Thu Nov 14, 2024 5:18 pm
by emperor-limitless
interesting, thanks
does this apply to strings? Or are strings a different story? I mean a string in a structure, example

Code: Select all

structure MyStruct
  Name$
  Age.i
EndStructure
one idea is to just use AllocateStructure all the time anyway regardless to avoid mistakes, however, I have no idea if that will have other problems, I'm aware that, at least, it'll have some overhead compared to using normal structures because heap is slower than stack, but I'm too inexperienced to know for sure.

Re: confusion about memory allocation with structures and dynamic elements

Posted: Fri Nov 15, 2024 12:10 am
by mk-soft
Structures with strings are also best allocated with AllocateStructure as the string is automatically released with FreeStructure.

AllocateMemory only for buffer and os api functions

Re: confusion about memory allocation with structures and dynamic elements

Posted: Fri Nov 15, 2024 9:47 am
by Fred
What's your use case ? You usually don't need these function in PB as you can rely to list/map/array to handle dynamic allocations

Re: confusion about memory allocation with structures and dynamic elements

Posted: Sat Nov 16, 2024 1:02 pm
by Olli

Code: Select all

AllocateStructure() = AllocateMemory() + InitializeStructure()

Code: Select all

FreeStructure() = ClearStructure() + FreeMemory()