Struct Array mit Entity Array - Dynamic

Posted By: Ayumi

Struct Array mit Entity Array - Dynamic - 01/01/17 12:34

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".
Posted By: Ch40zzC0d3r

Re: Struct Array mit Entity Array - Dynamic - 01/01/17 13:02

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
Posted By: Ayumi

Re: Struct Array mit Entity Array - Dynamic - 01/01/17 13:33

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.
Posted By: Ch40zzC0d3r

Re: Struct Array mit Entity Array - Dynamic - 01/01/17 19:15

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)
Posted By: Ayumi

Re: Struct Array mit Entity Array - Dynamic - 01/02/17 09:45

Are you sure?
Get the Error: Subscript require array type, because "ents" isn t an array Type.
Posted By: Ch40zzC0d3r

Re: Struct Array mit Entity Array - Dynamic - 01/02/17 12:27

I changed my original code a bit, you should rather store entity pointers instead of the whole entity struct grin
Posted By: Ayumi

Re: Struct Array mit Entity Array - Dynamic - 01/02/17 17:55

Ty but same error:D
Posted By: Superku

Re: Struct Array mit Entity Array - Dynamic - 01/16/17 16:45

Change
flares[i].ents[0]
to
(flares[i].ents)[0]
!
Posted By: Ayumi

Re: Struct Array mit Entity Array - Dynamic - 01/17/17 18:02

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.
© 2024 lite-C Forums