
Ich habe einen C-Code in Ubuntu 14.04.4 LTS geschrieben und kompiliert. Er funktioniert wie erwartet. Unter Windows läuft er zwar, zeigt aber am Ende einige Müllwerte an.
Der Code, den ich geschrieben habe, ist
#include <stdio.h>
#include <stdlib.h>
struct poly{
int coe[100];
};
void mul(struct poly *pr, int a, struct poly *p){
struct poly res;
int i;
for(i = 0; i < 100; i++){
res.coe[i] = 0;
int j;
for(j = 0; j <= i; j++){
res.coe[i] += ((*pr).coe[j])*((*(p + a)).coe[i - j]);
}
}
*pr = res;
}
void main(){
struct poly *p;
p = (struct poly *)malloc(100*sizeof(struct poly));
int n;
printf("no. of poly :");
scanf("%d", &n);
int i; int max = 0;
for(i = 0; i < n; i++){
int de;
printf("deg? of %d:", i+1);
scanf("%d", &de); max += de;
int j;
for(j = 0; j <= de; j++){
printf("co-eff of deg %d:", j);
scanf("%d", &p[i].coe[j]);
}
}
struct poly res;
struct poly *pr;
res = p[0];
pr = &res;
int fi;
for(fi = 1; fi < n; fi++){
mul(&res, fi, p);
}
for(i = 0; i < (max + 1); i++){
printf("%d\n", res.coe[i]);
}
}
und das Ergebnis in Windows ist
C:\Users\Sai\Documents\C++>gcc ac.c -o ac
C:\Users\Sai\Documents\C++>ac
no. of poly :3
deg? of 1:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
deg? of 2:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
deg? of 3:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
1
3
6
85067032
255201082
510403447
-1897503563
C:\Users\Sai\Documents\C++>
das Ergebnis in Ubuntu ist
sai@sai-Inspiron-7548:~$ gcc ac.c -o ac
sai@sai-Inspiron-7548:~$ ./ac
no. of poly :3
deg? of 1:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
deg? of 2:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
deg? of 3:2
co-eff of deg 0:1
co-eff of deg 1:1
co-eff of deg 2:1
1
3
6
7
6
3
1
sai@sai-Inspiron-7548:~$
So führen Sie dieses Programm unter Windows 10 ordnungsgemäß aus.
Stimmt etwas mit dem Code nicht, den ich geschrieben habe?
Danke schön.
Antwort1
Dies hätte zwar woanders gepostet werden sollen, aber hier ist die Lösung:
Immer initialisieren!
Sie initialisieren nicht das coe[100]
Array aller Elemente in Ihrem struct poly*
.
Du setzt in Deinen Schleifen zwar nur einen Teil der Einträge, Deine Mul-Funktion greift aber auch auf nicht initialisierte zu.
Ich gehe davon aus, dass der gcc unter Linux das irgendwie behebt, indem er sie standardmäßig als 0 initialisiert, der Standard definiert den Wert einer nicht initialisierten Ganzzahl jedoch als undefiniert.
MinGW initialisiert es nicht für Sie, sodass alles, was sich zu diesem Zeitpunkt im Speicher befindet, Ihre Ergebnisse durcheinanderbringt.
Edit: Natürlich solltest du auch den allokierten Speicher mit freigeben free(p);
!
So können Sie das Problem beheben:
#include <stdio.h>
#include <stdlib.h>
struct poly{
int coe[100];
};
void mul(struct poly *pr, int a, struct poly *p){
struct poly res;
int i;
for(i = 0; i < 100; i++){
res.coe[i] = 0;
int j;
for(j = 0; j <= i; j++){
res.coe[i] += ((*pr).coe[j])*((*(p + a)).coe[i - j]);
}
}
*pr = res;
}
void main(){
struct poly *p;
p = (struct poly *)malloc(100*sizeof(struct poly));
for(int k = 0; k < 100; k++)
for(int l = 0; l < 100; l++)
p[k].coe[l] = 0;
int n;
printf("no. of poly :");
scanf("%d", &n);
int i; int max = 0;
for(i = 0; i < n; i++){
int de;
printf("deg? of %d:", i+1);
scanf("%d", &de); max += de;
int j;
for(j = 0; j <= de; j++){
printf("co-eff of deg %d:", j);
scanf("%d", &p[i].coe[j]);
}
}
struct poly res;
struct poly *pr;
res = p[0];
pr = &res;
int fi;
for(fi = 1; fi < n; fi++){
mul(&res, fi, p);
}
for(i = 0; i < (max + 1); i++){
printf("%d\n", res.coe[i]);
}
free(p);
}