confusion about memory allocation with structures and dynamic elements

Just starting out? Need help? Post your questions and find answers here.
emperor-limitless
User
User
Posts: 11
Joined: Tue Nov 12, 2024 5:14 pm

confusion about memory allocation with structures and dynamic elements

Post 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:
SMaag
Enthusiast
Enthusiast
Posts: 303
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

Re: confusion about memory allocation with structures and dynamic elements

Post 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)

emperor-limitless
User
User
Posts: 11
Joined: Tue Nov 12, 2024 5:14 pm

Re: confusion about memory allocation with structures and dynamic elements

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6207
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: confusion about memory allocation with structures and dynamic elements

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: confusion about memory allocation with structures and dynamic elements

Post 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
Olli
Addict
Addict
Posts: 1200
Joined: Wed May 27, 2020 12:26 pm

Re: confusion about memory allocation with structures and dynamic elements

Post by Olli »

Code: Select all

AllocateStructure() = AllocateMemory() + InitializeStructure()

Code: Select all

FreeStructure() = ClearStructure() + FreeMemory()
Post Reply