c 프로그램은 Ubuntu에서 잘 작동하지만 MinGW를 사용하여 Windows에서 컴파일하고 실행하면 프로그램이 실행되지만 결과적으로 일부 쓰레기 값이 표시됩니다.

c 프로그램은 Ubuntu에서 잘 작동하지만 MinGW를 사용하여 Windows에서 컴파일하고 실행하면 프로그램이 실행되지만 결과적으로 일부 쓰레기 값이 표시됩니다.

Ubuntu 14.04.4 LTS에서 C 코드를 작성하고 컴파일했습니다. 예상대로 작동하지만 Windows에서는 실행 중이지만 결국에는 일부 가비지 값이 표시됩니다.

제가 작성한 코드는

#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]);
    }
}

Windows의 결과는 다음과 같습니다.

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

우분투에서의 결과는

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:~$ 

Windows 10에서 이 프로그램을 올바르게 실행하는 방법

내가 작성한 코드에 뭔가 문제가 있는 걸까요?

감사합니다.

답변1

이 내용은 다른 곳에 게시했어야 했지만 해결 방법은 다음과 같습니다.

항상 초기화하세요!

coe[100]의 모든 요소 배열을 초기화하지 않습니다 struct poly*.

루프에서는 일부 항목만 설정하지만 다중 기능은 초기화되지 않은 항목에도 액세스합니다.

나는 Linux의 gcc가 기본적으로 0으로 초기화하여 이를 처리한다고 가정합니다. 그러나 표준에서는 초기화되지 않은 정수 값을 정의되지 않은 것으로 정의합니다.

MinGW는 이를 초기화하지 않으므로 그 당시 메모리에 있는 것이 무엇이든 결과를 엉망으로 만듭니다.

편집: 물론 할당된 메모리도 free(p);! 로 해제해야 합니다.

문제를 해결하는 방법은 다음과 같습니다.

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

관련 정보