RTE (SIGSEGV: segmentation fault, probably incorrect memory access or stack overflow) 오류관련 질문

  • sjune007
    sjune007

    calloc하는 부분에서 오류가 발생하는 것 같은데 원인을 모르겠습니다.
    조언 부탁드려요.
    리눅스나 비쥬얼에서는 빌드가 잘되구요. codeforces 사이트에서도 문제 없이 돌어가는데 여기만 제출하면 런타임 오류가 발생하네요~

    // 보트크기를 동적으로 생성하는 부분
    board = (int **)calloc(h, sizeof(int));

    for (i = 0; i < h; i++)
        {
            *(board + i) = (int *)calloc((w + 1), sizeof(int));
        }
    
        input = (char *)malloc((w + 1) * sizeof(int));

    #include<stdio.h>
    #include<stdlib.h>
    
    #define LBLOCK 3
    
    int getCheck(int, int, int, int, int, int **, int);
    
    int main()
    {
        int n; // 테스트 갯수
    
        scanf("%d", &n);
    
        while (n--)
        {
            int h, w; // H : 높이 , W : 넓이
            char *input; // 입력받는 변수
            int chg; // 문자열로 들어온 변수를 Int형으로 변경한 변수
            int cnt = 0; // 보드내에 흰칸 갯수
            int total = 0; // 총 덮는 방법 수
            int i, j;
            int **board; // 보듲차원배열
    
            scanf("%d %d", &h, &w);
    
            // 보트크기를 동적으로 생성하는 부분
            board = (int **)calloc(h, sizeof(int));
    
            for (i = 0; i < h; i++)
            {
                *(board + i) = (int *)calloc((w + 1), sizeof(int));
            }
    
            input = (char *)malloc((w + 1) * sizeof(int));
    
            // 보드상태를 입력 받는 부분
            for (i = 0; i < h; i++)
            {
                scanf("%s", input);
    
                j = 0;
                while (*(input + j) != '\0')
                {
                    if (input[j] == '#') chg = -1;
                    else {
                        chg = 0;
                        cnt++;
                    }
    
                    *(*(board + i) + j) = chg;
    
                    j++;
                }
            }
    
            // 결과를 출력하는 부분
    
            total += getCheck(0, 0, h, w, cnt, board, 0);
    
            printf("%d\n", total);
    
            // 동적으로 생성된 보드를 초기화하는 부분
            for (i = 0; i < h; i++)
                free(*(board + i));
            free(board);
            free(input);
        }
    }
    
    int getCheck(int x, int y, int h, int w, int cnt, int **board, int lvl)
    {
        int i, j, sum = 0;
    
        if (cnt%LBLOCK) return 0;
        if (cnt / LBLOCK == lvl) return 1;
        if (x - 1 >= 0 && y - 1 >= 0 &&
            (board[x - 1][y] == 0 || board[x][y - 1] == 0)) return 0;
    
        // 보드상태를 입력 받는 부분
        for (i = x; i < h - 1; i++)
        {
            if (i > x) y = 0;
            for (j = y; j < w - 1; j++)
            {
    
                /* ┘ */
                if (i + 1 < h && j + 1 < w &&
                    board[i][j + 1] == 0 && board[i + 1][j] == 0 && board[i + 1][j + 1] == 0) {
                    board[i][j + 1] = board[i + 1][j] = board[i + 1][j + 1] = 1;
                    sum += getCheck(i, j, h, w, cnt, board, lvl + 1);
                    board[i][j + 1] = board[i + 1][j] = board[i + 1][j + 1] = 0;
                }
    
                if (board[i][j] != 0) continue;
    
                /* └ */
                if (i + 1 < h && j + 1 < w &&
                    board[i][j] == 0 && board[i + 1][j] == 0 && board[i + 1][j + 1] == 0) {
                    board[i][j] = board[i + 1][j] = board[i + 1][j + 1] = 1;
                    sum += getCheck(i, j, h, w, cnt, board, lvl + 1);
                    board[i][j] = board[i + 1][j] = board[i + 1][j + 1] = 0;
                }
                /* ┐ */
                if (i + 1 < h && j + 1 < w &&
                    board[i][j] == 0 && board[i][j + 1] == 0 && board[i + 1][j + 1] == 0) {
                    board[i][j] = board[i][j + 1] = board[i + 1][j + 1] = 1;
                    sum += getCheck(i, j, h, w, cnt, board, lvl + 1);
                    board[i][j] = board[i][j + 1] = board[i + 1][j + 1] = 0;
                }
                /* ┌ */
                if (i + 1 < h && j + 1 < w &&
                    board[i][j] == 0 && board[i + 1][j] == 0 && board[i][j + 1] == 0) {
                    board[i][j] = board[i + 1][j] = board[i][j + 1] = 1;
                    sum += getCheck(i, j, h, w, cnt, board, lvl + 1);
                    board[i][j] = board[i + 1][j] = board[i][j + 1] = 0;
                }
            }
        }
    
        return sum;
    }
    

    8년 전
1개의 댓글이 있습니다.
  • pushbell7
    pushbell7

    보통 메모리관련된 문제는 너무 많은 양의 동적할당이라던가 프로시져 콜이 많이 호출될 경우 발생하는 것 같아요
    예를들어 2차원 배열을 동적할당 할때 100000*100000이면 동적할당을 그만두고 실행에러를 내기도하고, 순환으로 작성한 프로시저를 만번가량 계속 호출하면 스택 오버플로우가 나오기도 하지요 그런 문제가 아닐까 추측해봅니다.


    8년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.