Page 1 of 1
Dynamic instances of Structures
Posted: Tue Dec 23, 2003 4:48 am
by SurreaL
I know you can dynamically allocate memory using the Memory commands within PB.. But I'm wondering about dynamically creating/deleting instances of Structures.
An example scenario: Lets say I wanted to have a structure to represent information of a player in a multiplayer game, and I wanted to create and delete said information structure whenever a new player joined or left..
I can figure out how to dynamically create a structure.. (Perhaps by creating a function that declares a structure, then returning the address of that struc to be used by the rest of the prog) Is there a way I can later delete this struct? Do I have to be concerned about Scope affecting this idea? (IE once the struct goes out of scope.. does it get deleted?)
Any help would be appreciated!
Posted: Tue Dec 23, 2003 5:49 am
by Karbon
Make a linked list of the structure type. Then you can add or remove items as needed.
Linked lists are always global..
Posted: Tue Dec 23, 2003 6:57 am
by SurreaL
Hm.. Ok. Sounds fairly similar to what I was doing in Blitz

I'll just have to port over my memory management routines to use Memory Banks which store Pointers to Structures to handle dynamically resizable random access based memory.
Cool

Thanks for the tip.
Posted: Tue Dec 23, 2003 12:47 pm
by freak
No need to do allocation stuff yourself here, PB's Linkedlist functions work
perfectly for such a tast.
Code: Select all
Structure PlayerStruct
x.l
y.l
Whatever.l
EndStructure
NewList Player.PlayerStruct() ; this creates the list, only needed once at start
; now, to add a player:
AddElement(Player())
Player()\X = 20
Player()\Y = 100
;...
; to select the current player, you can use:
SelectElement(Player(), ListIndex)
; or loop through them all, simply with
ForEach Player()
Debug Player()\X
Next Player()
; to remove the current player:
DeleteElement(Player())
; clear the list:
ClearList(Player())
; count the players:
NumPlayers = CountList(Player())
You see, it's pretty simple with those commands. Have a look
in the Linkedlist help chapter, you'll find some more commands there,
and also the detailed description on how they work.
Timo
Posted: Tue Dec 23, 2003 7:26 pm
by SurreaL
Hm.. as long as you can store pointers to those structs as created by the linked list in a memory bank.. and later dereference said pointer from a memory bank.. then all will be well

Posted: Tue Dec 23, 2003 10:25 pm
by freak
You can just get the pointer with *pointer = @Player() , and do whatever you want wit it.
Posted: Tue Dec 23, 2003 10:49 pm
by SurreaL
Very interesting indeed
But I believe I recall reading on another post somewhere that Linked lists cannot be used in DLL's.. I'll have to do some further testing methinks!
Posted: Tue Dec 23, 2003 11:01 pm
by Num3
Dumb question...
Is the memory leak problem solved !? 8O
Posted: Tue Dec 23, 2003 11:09 pm
by SurreaL
er. Even dumber question..
What memory leak? Or do you mean that by using Linked Lists of Structures you're only removing the pointer to the struct, thus not the actual memory used by the struc..?
I'm too used to Blitz.. where you explicitely have to call a function to Create and Delete their version of Structs ("Types").. I don't want to start a memory leak without 'em tho :/
Posted: Tue Dec 23, 2003 11:15 pm
by Num3
Well PB had / has !? a bug with linked list... Huge memory leak one
Whenever you create a linked list after a while of doing operations with it memory stops being freed... and starts getting wasted until it crashes the app...
Can anyone please confirm if this still stands true
Another option... Get more RAM

Posted: Tue Dec 23, 2003 11:30 pm
by freak
The memory leak thing is about using strings in Linkedlists, and it is not
solved yet AFAIK. You have to free the strings yourself by assigning them
a null string: "".
Linkedlists can be used in DLL's, but you have to put the NewList command
inside a Procedure as well, as it won't be called, when it is outside. You also
have to make sure, this is called only once, so put it the AttachProcess()
procedure for example.
Timo