gpt4 book ai didi

C 数组被覆盖?

转载 作者:行者123 更新时间:2023-11-30 21:20:38 25 4
gpt4 key购买 nike

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

所以第一次迭代失败了,但我确信我确实写入了 helper_glpk.col_bounds 的第一个案例在第 200-204 行,我做了 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时

最佳答案

这段代码是错误的:

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< 更正您的 malloc/.

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

关于C 数组被覆盖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36177967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com