C 数组被覆盖?

C Array being overwritten?

当我运行这个程序时:http://hastebin.com/asorawoluw.m

我在 GDB 中遇到这个错误:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000401f91 in resoudre (baie=...) at lineaire.c:291
291         printf("type[%d] : %d\n", i, helper_glpk.col_bounds[i]->type);

当我要求 gdb print i我得到:

 = 1

所以第一次迭代失败了,但我确定我确实在第 200-204 行写入了 helper_glpk.col_bounds 的第一个案例,并且我做了 malloc 所以没有办法(我认为?)我的数据正在被覆盖或删除。所以我不明白为什么会出现此错误。

编辑:这是最少的代码: 我的结构:

typedef struct Bounds Bounds;
struct Bounds
{
    int type;
    double lb;
    double ub;
};

typedef struct HelperGlpk HelperGlpk;
struct HelperGlpk
{
    double *matrix_coefs;
    double *obj_coefs;
    Bounds **row_bounds;
    Bounds **col_bounds;
    int *column_of_coef;
    int *row_of_coef;
    int cpt_coef;
    int cpt_contrainte;
};

我生成约束的函数:

void genere_contrainte_1(int i, int j, HelperGlpk *helper_glpk, Baie baie){         
    helper_glpk->col_bounds[index_ouverture_serveur(i)]->type = GLP_DB;
    helper_glpk->col_bounds[index_ouverture_serveur(i)]->lb = 0;
    helper_glpk->col_bounds[index_ouverture_serveur(i)]->ub = 1;

    helper_glpk->cpt_coef++;

    helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)]->type = GLP_LO;
    helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)]->lb = 0;
    helper_glpk->col_bounds[index_connexion(i, j, baie.nbr_serveur)]->ub = 0;

    helper_glpk->cpt_coef++;
}

主程序是:

void resoudre(Baie baie){

    glp_prob *lp;

    const int nbr_rows = baie.nbr_client + baie.nbr_serveur * baie.nbr_client; // nombre de contrainte
    const int nbr_colums = baie.nbr_serveur + baie.nbr_serveur * baie.nbr_client; // nombre de variable
    const int nbr_coefs = 3 * baie.nbr_serveur * baie.nbr_client;

    int i, j;

    HelperGlpk helper_glpk;

    helper_glpk.matrix_coefs = malloc((nbr_coefs + 1) * sizeof(double));
    helper_glpk.matrix_coefs[0] = 0;

    helper_glpk.obj_coefs = malloc((nbr_colums + 1) * sizeof(double));
    helper_glpk.obj_coefs[0] = 0;

    helper_glpk.column_of_coef = malloc((nbr_colums + 1) * sizeof(int));
    helper_glpk.column_of_coef[0] = 0;

    helper_glpk.row_of_coef = malloc((nbr_rows + 1) * sizeof(int)); 
    helper_glpk.row_of_coef[0] = 0;

        helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds *));

for (int index = 0; index <= nbr_colums; index++)
{
    helper_glpk.col_bounds[index] = malloc(sizeof(Bounds));
}

helper_glpk.row_bounds = malloc((nbr_rows + 1) * sizeof(Bounds *));

for (int index = 0; index <= nbr_rows; index++)
{
    helper_glpk.row_bounds[index] = malloc(sizeof(Bounds));
}

    helper_glpk.cpt_coef = 1;

    for(i = 1; i <= baie.nbr_serveur; i++)
        for(j = 1; j <= baie.nbr_client; j++)
            genere_contrainte_1(i, j, &helper_glpk, baie);

    for(i = 1; i <= nbr_colums; i++)
        printf("type[%d] : %d\n", i, helper_glpk.col_bounds[i]->type);

    for(j = 1; j <= baie.nbr_client; j++)
        genere_contrainte_2(j, &helper_glpk, baie.nbr_serveur);

我遇到的错误是在调用 generate_contrainte_1

后尝试打印时

这段代码是错误的:

helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds));

你需要修复它(前提是你需要 nbr_colums + 1 个元素):

helper_glpk.col_bounds = malloc((nbr_colums + 1) * sizeof(Bounds *));
for (int index = 0; index < nbr_colums + 1; index++)
{
    helper_glpk.col_bounds[index] = malloc(sizeof(Bounds));
}

我没有检查其余代码,可能还有其他错误。

编辑:也许您不需要 for 循环,具体取决于您的 genere_contrainte_1 所做的事情,但您需要使用正确的 sizeof.[=22 更正您的 malloc =]

Edit2:我读了你的 genere_contrainte_1,你肯定需要所有这些 malloc。但我真的怀疑你需要 row_boundscol_bounds 才能成为 Bounds **,在我看来 Bounds * 就可以了,这样一个 malloc每个字段就够了。