I have a very large structure, and it seems to be over the limit, what would be the best way to work this out?
Error Message: total size of array must not exceed 0x7fffffff bytes
structure occurring C2148 when compiling
struct listen_instance {
struct instance_l instance[2000];
};
I need all this information contained in struct
Structure code below:
struct sk_instance {
int id;
int rg[10];
int num[10];
int ht;
int ir;
int el[10];
int nt;
int sl[10];
int blc[10];
int cst;
int cdr;
int mxc[10];
int mcst[10];
int wd[10];
int dl[10];
int cd[10];
int vr[10];
int cz[10];
int tj[10];
int hr[10];
int kg[10];
int sr[10];
int nb[10];
int cv[10];
int op[10];
};
struct instance_cl {
int enable_cl;
};
struct instance_ef {
int ef_time;
};
struct instance_l {
struct sk_instance sk[1600];
struct instance_ef ef[700];
struct instance_cl cl[250];
};
struct listen_instance {
struct instance_l instance[2000];
};
I use as follows in my code
Example 1:
for(int i = 0; i < 10; i++)
Cfi->lis->instance[t].sk[id].rg[i] = 1;
Example 2:
Cfi->lis->instance[t].ef[200].ef_time = 1000; // 1000 = 1 second
Example 3:
Cfi->lis->instance[t].cl[10].enable_cl = 1; // true
You will simply have to switch to dynamic allocation. You could start with just this:
struct listen_instance {
struct instance_l *instance;
};
struct listen_instance init_listen_instance()
{
struct listen_instance ret;
ret.instance = malloc(2000 * sizeof(*(ret.instance)));
if(!ret.instance) { /* Handle error */ }
return ret;
}
Then you can invoke struct listen_instance myVar = init_listen_instance();
and then you can use myVar
the same way with one exception. sizeof(myVar.instance)
will not yield the same result.
If that's not enough, do the same for other structs too.
struct instance_l {
struct sk_instance *sk;
struct instance_ef *ef;
struct instance_cl *cl;
};
struct instance_l init_instance_l()
{
struct instance_l ret;
ret.sk = malloc(1600 * sizeof(*(ret.sk)));
if(!ret.sk) { /* Handle error */ }
ret.ef = malloc(700 * sizeof(*(ret.ef)));
if(!ret.sk) { /* Handle error */ }
ret.cl = malloc(250 * sizeof(*(ret.cl)));
if(!ret.sk) { /* Handle error */ }
return ret;
}
It can be wise to also write free functions when doing like this. Here is an example.
void free_instance_l(struct instance_l x)
{
free(x.sk);
free(x.ef);
free(x.cl);
}
User contributions licensed under CC BY-SA 3.0