c - 编译时出现C2148的结构

标签 c data-structures

我有一个非常大的结构,它似乎超出了限制,解决这个问题的最佳方法是什么?

错误消息:数组的总大小不得超过 0x7fffffff 字节

编译时出现C2148的结构

struct listen_instance {
   struct instance_l instance[2000];
};

我需要结构中包含的所有这些信息

结构代码如下:

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];
};

我在代码中使用如下

示例1:

for(int i = 0; i < 10; i++)
   Cfi->lis->instance[t].sk[id].rg[i] = 1;

示例2:

Cfi->lis->instance[t].ef[200].ef_time = 1000; // 1000 = 1 second

示例3:

Cfi->lis->instance[t].cl[10].enable_cl = 1; // true

最佳答案

您只需切换到动态分配即可。你可以从这个开始:

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;
}

然后您可以调用struct Listen_instance myVar = init_listen_instance();,然后您可以以相同的方式使用myVar,但有一个异常(exception)。 sizeof(myVar.instance) 不会产生相同的结果。

如果这还不够,请对其他结构执行相同的操作。

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;
}

这样做时最好也编写自由函数。这是一个例子。

void free_instance_l(struct instance_l x)
{
    free(x.sk);
    free(x.ef);
    free(x.cl);
}

关于c - 编译时出现C2148的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58781520/

相关文章:

c - 在 C 微 Controller 中设置位

c++ - 对具有两列并支持 CRUD 操作的 C++ 数据结构的建议

python - 为什么 n=[1,2,3,4,5,6,7,8],n[ :6:-2] is [8] in Python?

c++ - 如何使用大数组?

c - K&R快速排序问题

c++ - 用于为 C/C++/脚本代码着色的优秀 WordPress 扩展是什么?

c - 如何创建 128 位变量并将其从 Chapel 程序传递到第三方 C 库?

使用 valgrind 测试时,将节点添加到链表末尾的 C 程序面临无效写入大小错误

java - 作业问题的有效算法

c++ - 数据结构中的 MST 和唯一性问题已解决 Ex?