我正在解决leetcode上的n-queen问题。但它在leetcode上产生堆溢出错误。
但在我的电脑上,当我一次输入时,它给出了正确的答案,但多次输入时,它给出了分割错误:11个错误。
当我不打印完整的棋盘时。只需打印不同可能解决方案的编号。那也行。

#include <stdio.h>
#include <stdlib.h>

#define bool int
#define true 1
#define false 0

//checking positioned queens
bool checkPlacedQueens(char **board, int queeni, int queenj, int n) {
    int i=queeni;
    int j=queenj;

    //checking complete row
    for(int i=queeni; i>=0; i--) {
        if(board[i][j] == 'Q')
            return false;
    }

    i=queeni;
    j=queenj-1;
    //checking left diagonal
    while(i>=0 && j>=0) {
        if(board[i--][j--] == 'Q')
            return false;
    }

    i=queeni;
    j=queenj+1;
    //checking right diagonal
    while(i>=0 && j<=n) {
        if(board[i--][j++] == 'Q')
            return false;
    }

    return true;
}

char ***placeQueens(char **board, int queenI, int n, int *returnSize, char ****result) {

    //all queens are on their correct position
    if(queenI == n) {
        (*returnSize)++;

        /*
            reallocating the memory to save all the outputs in 3D
            array
        */
        (*result) = (char ***) realloc(*result, sizeof(char **)*(*returnSize));
        (*result)[*returnSize-1] = (char **)malloc(sizeof(char *)*n);

        for(int i=0; i<n; i++) {
            (*result)[*returnSize-1][i] = (char *)malloc(sizeof(char)*n);
        }

        for(int i=0; i<n; i++) {
            for(int j=0; j<n; j++) {
                (*result)[*returnSize-1][i][j] = board[i][j];
            }
        }

        return *result;
    }//if

    for(int j=0; j<n; ++j) {
        char save = board[queenI][j];
        board[queenI][j] = 'Q';
        if(checkPlacedQueens(board, queenI-1, j, n)) {
            placeQueens(board, queenI+1, n, returnSize, result);
        }
        board[queenI][j] = save;
    }//for Loop

    return *result;
}//function

char *** solveNQueens(int n, int* returnSize) {
    char **board;
    char ***result = (char ***)malloc(sizeof(char **));

    board = (char **)malloc(sizeof(char *)*n);

    for(int i=0; i<n; i++) {
        board[i] = (char *)malloc(sizeof(char)*n);
    }

    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            board[i][j] = '.';
        }
    }

    placeQueens(board, 0, n, returnSize, &result);

    for(int i=0; i<n; i++)
        free(board[i]);
    free(board);

    return result;
}//char

int main(void) {
    int returnSize=0;
    int n=4;
    char ***arr;

    while(n<10) {
        arr = solveNQueens(n, &returnSize);

        for(int i=0; i<returnSize; ++i) {
            for(int j=0; j<n; j++) {
                for(int k=0; k<n; k++) {
                    printf("%c", arr[i][j][k]);
                }
                printf("\n");
            }
            printf("\n");
        }
        printf("\n\n\n");

        for(int i=0; i<returnSize; ++i) {
            for(int j=0; j<n; j++) {
               free(arr[i][j]);
            }
            free(arr[i]);
        }

        free(arr);
        arr=NULL;
        n++;
    }
}//main

最佳答案

Yoor代码具有越界读取非法内存访问。它的现场测试在这里,
以下是越界读取的诊断消息。
===Stensal DTS消息的开始==(56.133)==此处复制开始======
[越界读取]由Stensal DTS检测到。
继续执行可能导致未定义的行为,中止!

+--------------------------------------------------------------------------+
| Reading 1 bytes from 0x80c5054 will read undefined values.
|
| The object to-be-read (start:0x80c5050, size:4 bytes) is allocated at
|     file:/prog.c::83, 28
|
|  0x80c5050               0x80c5053
|  +------------------------+
|  | the object  to-be-read |......
|  +------------------------+
|                            ^~~~~~~~~~
|        the read starts at 0x80c5054 that is right after the object end.
|
| Stack trace (most recent call first) of the read.
| [1]  file:/prog.c::31, 12
| [2]  file:/prog.c::67, 12
| [3]  file:/prog.c::68, 13
| [4]  file:/prog.c::92, 5
| [5]  file:/prog.c::107, 15
| [6]  [libc-start-main]
+--------------------------------------------------------------------------+

===Stensal DTS消息的结尾=======copy End here=========

关于c - 即使在分配内存时也会出现堆溢出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54813576/

10-11 15:59