英文:
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 < 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 , adresse = %p\n",(*k),k);
(*k)= (*k)+1;
}
}
}
printf("k final=%d, adresse = %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));
}
// 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<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 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));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论