Dynamic instances of Structures

Just starting out? Need help? Post your questions and find answers here.
SurreaL
User
User
Posts: 43
Joined: Tue Dec 23, 2003 3:06 am

Dynamic instances of Structures

Post 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!
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Make a linked list of the structure type. Then you can add or remove items as needed.

Linked lists are always global..
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
SurreaL
User
User
Posts: 43
Joined: Tue Dec 23, 2003 3:06 am

Post 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.
freak
PureBasic Team
PureBasic Team
Posts: 5941
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
SurreaL
User
User
Posts: 43
Joined: Tue Dec 23, 2003 3:06 am

Post 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 :D
freak
PureBasic Team
PureBasic Team
Posts: 5941
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

You can just get the pointer with *pointer = @Player() , and do whatever you want wit it.
quidquid Latine dictum sit altum videtur
SurreaL
User
User
Posts: 43
Joined: Tue Dec 23, 2003 3:06 am

Post 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!
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Dumb question...

Is the memory leak problem solved !? 8O
SurreaL
User
User
Posts: 43
Joined: Tue Dec 23, 2003 3:06 am

Post 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 :/
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post 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... :cry:

Can anyone please confirm if this still stands true :?: :?:

Another option... Get more RAM :mrgreen:
Last edited by Num3 on Tue Dec 23, 2003 11:41 pm, edited 1 time in total.
freak
PureBasic Team
PureBasic Team
Posts: 5941
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
Post Reply