结构内部的C元素不保留赋值

时间:2016-03-23 14:17:00

标签: c pointers linear-programming glpk

这是我的结构:

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

这是我的函数generate_contrainte_1:

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

}

以下是我如何调用generate_contrainte_1:

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

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

问题在于内部的值

helper_glpk->col_bounds[index_ouverture_serveur(i)].type

不会保存在我的函数之外,当我尝试访问它时,我得到一个错误的值。

我该如何解决这个问题?

编辑:

int index_ouverture_serveur(int i){
    return i;
}

int index_connexion(int i, int j, size_t nbr_serveur){
    return j * nbr_serveur + i;
}

编辑2: 我添加了一些printf以了解错误的来源,它对我没有帮助,但它可能对您有所帮助:

    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;
    printf("col_bounds[%d].type = %d\n", index_ouverture_serveur(i), helper_glpk->col_bounds[index_ouverture_serveur(i)].type);

    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;

    printf("col_bounds[%d].type = %d\n", index_connexion(i, j, baie.nbr_serveur), helper_glpk->col_bounds[index_ouverture_serveur(i)].type);

这是我得到的:

col_bounds[1].type = 4
col_bounds[51].type = 4
col_bounds[1].type = 4
col_bounds[101].type = 4
col_bounds[1].type = 4
col_bounds[151].type = 4
col_bounds[1].type = 4
[...]
col_bounds[4900].type = 4
col_bounds[50].type = 4
col_bounds[4950].type = 4
col_bounds[50].type = 4
col_bounds[5000].type = 4
col_bounds[50].type = 4
col_bounds[5050].type = 4

现在我在我的函数之外执行printf:

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

这就是我得到的:

type[1] : 2555
type[2] : 2558
type[3] : 2561
type[4] : 2564
type[5] : 2567
type[6] : 2570
type[7] : 2573
type[8] : 2576
type[9] : 2579
type[10] : 2582
type[11] : 2585
type[12] : 2588
type[13] : 2591
type[14] : 2594
type[15] : 2597
type[16] : 2600
type[17] : 2603
type[18] : 2606
type[19] : 2609
type[20] : 2612
type[21] : 2615
type[22] : 2618
type[23] : 2621
type[24] : 2624
type[25] : 2627
type[26] : 2630
type[27] : 4
type[28] : 4
type[29] : 4
type[30] : 4
[...]
type[5042] : 2
type[5043] : 2
type[5044] : 2
type[5045] : 2
type[5046] : 2
type[5047] : 2
type[5048] : 2
type[5049] : 2
type[5050] : 2

0 个答案:

没有答案