【问题标题】:C Array being overwritten? [closed]C 数组被覆盖? [关闭]
【发布时间】:2016-03-23 12:17:52
【问题描述】:

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

【问题讨论】:

  • 请在此处粘贴适当的最小代码
  • 假设它是forwhile 循环,你不认为i 应该以0 开头。
  • helper_glpk.col_bounds 的类型/声明是什么? - 好的,刚刚在您的 pastebin 链接中看到它。但是提供 pastebin 链接并不是使用 SO 的方式。
  • first:在此处发布相关代码。第二:用英文写代码,让不懂法语的人也能看懂。
  • 顺便说一句:您的代码中没有任何内容是objective-c。它是普通的 C。

标签: c pointers linear-programming segmentation-fault glpk


【解决方案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));
}

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

编辑:根据 genere_contrainte_1 所做的事情,也许您不需要 for 循环,但您需要使用正确的 sizeof 更正您的 malloc

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

【讨论】:

  • 一开始是Bounds *,但是我想在函数内部修改col_bounds和row_bounds,所以我想我必须让它们成为指针?
  • 我刚刚按照你说的更新了我的代码,我现在有 Bounds *,但似乎我的结构没有保存,因为在我的函数中我有 helper_glpk->col_bounds[1].type = 1个;在外面我得到错误:type = 216;无效的列类型
  • 你需要这样调用你的函数:my_function(helper_glpk-&gt;col_bounds + i) 并且my_function 的参数必须是Bound *
  • 没有别的办法吗?我的函数已经有 helper_glpk 作为参数,并且需要 helper_glpk 的其他元素,所以我认为你的解决方案不是很好。如果我选择 Bounds ** 它应该解决我的问题吗?
  • 对不起,我之前的评论是完全错误的。要使用Bounds * 而不是Bounds **,您需要在genere_contrainte_1 函数中使用helper_glpk-&gt;col_bounds[...].type
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2014-05-23
  • 2012-05-09
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多