Variable value resetting outside of loop.

huangapple go评论63阅读模式
英文:

Variable value resetting outside of loop

问题

I encountered a memory problem i do not understand (probably just me being dumb).
I initialize a pointer to an int in cw(). I give it to cw_saving, where i increment it, here up to 42. Everything goes well in the loop, but when i get out, k is set to 7. I just don't get it.

typedef struct {
    float saving;
    int debut;
    int fin;
} Savings;

void cw_savings(float** matrice, int n, int n_hangar, Savings* savings, int * k) {
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < n; j++) {
            if (i == j) {
                continue;
            } else {
                savings[(*k)].saving = matrice[i][n_hangar] + matrice[n_hangar][j] - matrice[i][j];
                savings[(*k)].debut = i;
                savings[(*k)].fin = j;
                printf("k=%d , address = %p\n", (*k), k);
                (*k) = (*k) + 1;
            }
        }
    }
    printf("k final=%d, address = %p\n", (*k), k);
    // QuickSort(savings, 0, n - 2);
    qsort(savings, (*k), sizeof(Savings), comp);
}

int** cw(float** matrice, int n, int n_hangar, int vehicule_capacity, int* poids) {
    Savings* saving = calloc(((n - 1) * (n - 1) - n - 1), sizeof(Savings));
    int* k = calloc(1, sizeof(int));
    (*k) = 0;
    cw_savings(matrice, n, n_hangar, saving, k);
    int** res = initialisation(n - 1);
    cw_merge(res, saving, vehicule_capacity, poids, n - 1, (*k));
    free(saving);
    return res;
}

int main() {
    int n = 8;
    int n_hangar = 0;
    int vehicule_capacity = 30;
    int poids[] = {6, 8, 3, 9, 1, 3, 5};
    float** matrice = calloc((n), sizeof(float*));

    for (int i = 0; i < n; i++) {
        matrice[i] = (float*)calloc(n, sizeof(float));
    }
    // Initialize and fill the matrix with dummy values
    float t1[] = {0, 214.1, 378.2, 361.7, 231.7, 523.4, 308.8, 299.7};
    float t2[] = {163.2, 0, 164.1, 311.5, 284.1, 393.8, 309.1, 203.6};
    float t3[] = {321.1, 157.9, 0, 371.5, 344.1, 539.5, 369.1, 263.6};
    float t4[] = {344.1, 303., 311.5, 0, 215.7, 595.6, 133.2, 121.1};
    float t5[] = {222.1, 268.8, 277.3, 221.7, 0, 595, 168.8, 159.7};
    float t6[] = {497.2, 421.8, 518.2, 642.51, 638.2, 0, 663.2, 581.3};
    float t7[] = {299.5, 296.1, 304.6, 160.1, 171.1, 622.3, 0, 119.6};
    float t8[] = {286.6, 187.4, 195.9, 120.1, 160.5, 513.6, 121.0, 0};

    for (int i = 0; i < n; i++) {
        matrice[0][i] = t1[i];
        matrice[1][i] = t2[i];
        matrice[2][i] = t3[i];
        matrice[3][i] = t4[i];
        matrice[4][i] = t5[i];
        matrice[5][i] = t6[i];
        matrice[6][i] = t7[i];
        matrice[7][i] = t8[i];
    }

    int** resultat = cw(matrice, n, n_hangar, vehicule_capacity, poids);

    for (int i = 0; i < n; i++) {
        free(matrice[i]);
    }
    free(matrice);
    printf("[");
    for (int i = 0; i < n - 1; i++) {
        printf("%d", resultat[0][i]);
    }
    printf("]");
    printf("[");
    for (int i = 0; i < n - 1; i++) {
        if (resultat[0][i] == 0) {
            continue;
        }
        printf("[");
        for (int j = 0; j < resultat[0][i] + 2; j++) {
            printf("%d,", resultat[i + 1][j]);
        }
        printf("]");
        free(resultat[i + 1]);
    }
    printf("]");
    free(resultat[0]);
    free(resultat);

    return 0;
}
英文:

I encountered a memory problem i do not understand (probably just me beeing dumb).
I initialize a pointer to an int in cw(). I give it to cw_saving, where i increment it, here up to 42. Everything goes well in the loop, but when i get out, k is set to 7. I just don't get it.here are the prints

typedef struct {
    float saving;
    int debut;
    int fin;
} Savings;

void cw_savings(float** matrice, int n, int n_hangar, Savings* savings,int * k) {
    for (int i = 1; i &lt; n; i++) {
        for (int j = 1; j &lt; n; j++) {
            if (i == j) {
                continue;
            } else {
                savings[(*k)].saving = matrice[i][n_hangar] + matrice[n_hangar][j] - matrice[i][j];
                savings[(*k)].debut = i;
                savings[(*k)].fin = j;
                printf(&quot;k=%d , adresse = %p\n&quot;,(*k),k);
                (*k)= (*k)+1;
            }
        }
    }
    printf(&quot;k final=%d, adresse = %p\n&quot;,(*k),k);
    //QuickSort(savings, 0, n - 2);
    qsort(savings,(*k),sizeof(Savings),comp);
}

int** cw(float** matrice, int n,int n_hangar, int vehicule_capacity,int* poids){
    Savings* saving = calloc(((n - 1) * (n - 1) - n-1),sizeof(Savings));
    int* k = calloc(1,sizeof(int));
    (*k) = 0;
    cw_savings(matrice, n, n_hangar, saving,k);
    int** res = initialisation(n-1);
    cw_merge(res, saving ,vehicule_capacity,poids, n-1,(*k));
    free(saving);
    return res;
}

int main() {
    int n = 8; 
    int n_hangar = 0; 
    int vehicule_capacity = 30;
    int poids[] = {6,8,3,9,1,3,5};
    float** matrice = calloc((n), sizeof(float*));  

    for (int i = 0; i &lt; n; i++) {
        matrice[i] = (float*)calloc(n,sizeof(float));
    }
    // Initialiser et remplir la matrice avec des valeurs factices
    float t1 []= {0, 214.1, 378.2, 361.7, 231.7, 523.4, 308.8, 299.7};
    float t2 []= {163.2, 0, 164.1, 311.5, 284.1, 393.8, 309.1, 203.6};
    float t3 []= {321.1, 157.9, 0, 371.5, 344.1, 539.5, 369.1, 263.6};
    float t4 []= {344.1, 303., 311.5, 0, 215.7, 595.6, 133.2, 121.1};
    float t5 []= {222.1, 268.8, 277.3, 221.7, 0, 595, 168.8, 159.7};
    float t6 []= {497.2, 421.8, 518.2, 642.51, 638.2, 0, 663.2, 581.3};
    float t7 []= {299.5, 296.1, 304.6, 160.1, 171.1, 622.3, 0, 119.6};
    float t8 []= {286.6, 187.4, 195.9, 120.1, 160.5, 513.6, 121.0, 0};

    for (int i =0; i&lt;n;i++){
        matrice[0][i] = t1[i];
        matrice[1][i] = t2[i];
        matrice[2][i] = t3[i];
        matrice[3][i] = t4[i];
        matrice[4][i] = t5[i];
        matrice[5][i] = t6[i];
        matrice[6][i] = t7[i];
        matrice[7][i] = t8[i];
    }

    int** resultat = cw(matrice,n,n_hangar,vehicule_capacity,poids);

    for (int i = 0; i &lt; n; i++) {
        free(matrice[i]);
    }
    free(matrice);
    printf(&quot;[&quot;);
    for (int i =0; i&lt;n-1;i++){
        printf(&quot;%d&quot;,resultat[0][i]);
    }
    printf(&quot;]&quot;);
    printf(&quot;[&quot;);
    for (int i =0;i&lt;n-1;i++){
        if (resultat[0][i] == 0){
            continue;
        }
        printf(&quot;[&quot;);
        for (int j = 0;j&lt;resultat[0][i]+2;j++){
            printf(&quot;%d,&quot;,resultat[i+1][j]);
        }
        printf(&quot;]&quot;);
        free(resultat[i+1]);
    }
    printf(&quot;]&quot;);
    free(resultat[0]);
    free(resultat);

    return 0;
}

I tried everything, even changing pc and compiling online

答案1

得分: 1

You allocated saving for 40 elements:

Savings* saving = calloc(((n - 1) * (n - 1) - n-1),sizeof(Savings));
(8 - 1) * (8 - 1) - 8-1) = 49 - 8 - 1 = 40

So valid indices are [0..39]. But you access savings[(*k)] with index 40 which is out of bounds. It causes undefined behavior.

Probably you want n-1 in parentheses:

Savings* saving = calloc(((n - 1) * (n - 1) - (n-1)),sizeof(Savings));
英文:

You allocated saving for 40 elemets:

Savings* saving = calloc(((n - 1) * (n - 1) - n-1),sizeof(Savings));
(8 - 1) * (8 - 1) - 8-1) = 49 - 8 - 1 = 40

so valid indicies are [0..39]. But you access savings[(*k)] with index 40 which is out of bounds. It causes undefined behavior.

Probably you want n-1 in parentheses:

Savings* saving = calloc(((n - 1) * (n - 1) - (n-1)),sizeof(Savings));

huangapple
  • 本文由 发表于 2023年6月11日 23:24:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451167.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定