Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (Imhotep, opm), 785 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Struct Array mit Entity Array - Dynamic #463844
01/01/17 12:34
01/01/17 12:34
Joined: Oct 2008
Posts: 679
Germany
Ayumi Offline OP
User
Ayumi  Offline OP
User

Joined: Oct 2008
Posts: 679
Germany
Hey,

i would like to expand dynamically an array of structs with an array of Entitys(or any other)....separated from each other.

How to without memory leaks and Errors?

Code:
typedef struct Flare
{ 
  int Count;
  ENTITY* Ents[1]; // dynamic
} Flare;

Flare* Flares[1]; // -> dynamic

void AllocateNew()
{
   Flares = sys_malloc(sizeof(Flare) * 20);
   // Later in Code:
   Flares[0].Ents = sys_malloc(sizeof(Flare) + (count * sizeof(ENTITY)));
}



Get an unknown Error, if loop through "Ents".

Re: Struct Array mit Entity Array - Dynamic [Re: Ayumi] #463846
01/01/17 13:02
01/01/17 13:02
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
Code:
#define MAX_FLARES 32

typedef struct _FLARE
{ 
  int count;
  ENTITY **ents;
} FLARE;

FLARE *flares = 0;

...

flares = (FLARE*)malloc(sizeof(FLARE) * MAX_FLARES);
if(!flares) //errorhandling

int i;
for(i = 0; i < MAX_FLARES; i++)
{
  flares[i].count = 64;
  flares[i].ents = (ENTITY**)malloc(sizeof(ENTITY*) * flares[i].count);
  if(!flares[i].ents) //errorhandling

  flares[i].ents[0] = ent_create(..);
}



To enlarge the array(s) use realloc

Last edited by Ch40zzC0d3r; 01/02/17 12:26.
Re: Struct Array mit Entity Array - Dynamic [Re: Ch40zzC0d3r] #463848
01/01/17 13:33
01/01/17 13:33
Joined: Oct 2008
Posts: 679
Germany
Ayumi Offline OP
User
Ayumi  Offline OP
User

Joined: Oct 2008
Posts: 679
Germany
Thanks, have forgot ent_create but with your code, i get an memory area Error, if shut down the engine.(with and without Struct array)
And i have to set "[1]" by Ents, because the "subscript" error.

Last edited by Ayumi; 01/01/17 13:33.
Re: Struct Array mit Entity Array - Dynamic [Re: Ayumi] #463859
01/01/17 19:15
01/01/17 19:15
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
This code is 100% correct.
Check what sizeof(ENTITY) and sizeof(FLARE) is and see if its correct.
If nothing works simply use WinAPIs (CreateHeap, HeapAlloc, HeapFree)

Re: Struct Array mit Entity Array - Dynamic [Re: Ch40zzC0d3r] #463867
01/02/17 09:45
01/02/17 09:45
Joined: Oct 2008
Posts: 679
Germany
Ayumi Offline OP
User
Ayumi  Offline OP
User

Joined: Oct 2008
Posts: 679
Germany
Are you sure?
Get the Error: Subscript require array type, because "ents" isn t an array Type.

Re: Struct Array mit Entity Array - Dynamic [Re: Ayumi] #463871
01/02/17 12:27
01/02/17 12:27
Joined: Oct 2011
Posts: 1,082
Germany
C
Ch40zzC0d3r Offline
Serious User
Ch40zzC0d3r  Offline
Serious User
C

Joined: Oct 2011
Posts: 1,082
Germany
I changed my original code a bit, you should rather store entity pointers instead of the whole entity struct grin

Re: Struct Array mit Entity Array - Dynamic [Re: Ch40zzC0d3r] #463883
01/02/17 17:55
01/02/17 17:55
Joined: Oct 2008
Posts: 679
Germany
Ayumi Offline OP
User
Ayumi  Offline OP
User

Joined: Oct 2008
Posts: 679
Germany
Ty but same error:D

Re: Struct Array mit Entity Array - Dynamic [Re: Ayumi] #464044
01/16/17 16:45
01/16/17 16:45
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Change
flares[i].ents[0]
to
(flares[i].ents)[0]
!


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: Struct Array mit Entity Array - Dynamic [Re: Superku] #464050
01/17/17 18:02
01/17/17 18:02
Joined: Oct 2008
Posts: 679
Germany
Ayumi Offline OP
User
Ayumi  Offline OP
User

Joined: Oct 2008
Posts: 679
Germany
Currently there is an entity in a struct. The struct is expanded dynamically in a second struct. That s my solution and it works.

Thanks Superku, i will try it later.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1