C数组被覆盖?

时间:2016-03-23 12:17:52

标签: c pointers linear-programming sigsegv glpk

当我运行此程序时: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 = 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

后尝试printf时

1 个答案:

答案 0 :(得分:2)

此代码错误:

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

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

修改:根据genere_contrainte_1的内容,您可能不需要for循环,但需要使用右侧malloc更正sizeof

Edit2:我看了你的genere_contrainte_1,你肯定需要所有这些malloc。但我真的怀疑你需要row_boundscol_bounds Bounds **,在我看来Bounds *会很好,那就是malloc每个领域都足够了。