|
2 registered members (Imhotep, opm),
785
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Struct Array mit Entity Array - Dynamic
#463844
01/01/17 12:34
01/01/17 12:34
|
Joined: Oct 2008
Posts: 679 Germany
Ayumi
OP
User
|
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?
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
Ch40zzC0d3r
Serious User
|
Serious User
Joined: Oct 2011
Posts: 1,082
Germany
|
#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
OP
User
|
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.
|
|
|
|
|
|