0

在我们的c课中,老师给了我们一个小项目来构建一个“黑白棋”游戏。我在构建电路板时遇到了麻烦。

#define Size 8
int main()
{
    char Board[Size][Size] = { {" "} };
    resetBoard(Board);
    printBoard(Board);
    printf("\n");
    getch();
}
void resetBoard(int** board)
{
    for (size_t i = 0; i < Size; i++)
    {
        for (size_t j = 0; j < Size; j++)
        {
            *(board + (i * Size + j)) = 'x';
        }
    }
}
void printBoard(int board[Size][Size])
{
    for (size_t i = 0; i < Size; i++)
    {
        for (size_t j = 0; j < Size; j++)
        {
            printf("%c", board[i][j]);
            printf(" ");
        }
        printf("\n");
    }
}

我检查了程序,程序得到:

运行时检查失败 #2 - 变量“Board”周围的堆栈已损坏

当它更改第三行的第一个 X 时。例如,如果我将程序运行到第 2 行 (16) 的末尾,我不会收到此错误。

4

4 回答 4

4

我认为将板子初始化为 char 类型并在函数中使用指针和 int 类型的数组可能存在问题。Char 的大小为 1 个字节,而 int 的大小取决于平台(大多数情况下为 4 个字节)。这将导致操作和循环数组时出现内存问题。

在您的情况下,看起来您在 2 行之后循环了整个分配的内存,因为您使用了 int 类型的指针。在您的情况下, Int 可能比 char 大 4 倍,导致循环遍历 char 类型的整个数据结构的速度比您预期的快 4 倍。

于 2018-11-28T19:08:23.490 回答
1

你的代码中有一堆错误。请参阅内联注释。

    //Use all capitals for defines
#define BOARD_SIZE 8

//Just reset the whole array to spaces.. No need to traverse byte by byte.
void resetBoard(char* board) {
    //memset version
    //memset(board, ' ', (BOARD_SIZE*BOARD_SIZE)*sizeof(char));

    //non memset version
    for (int i=0; i<(BOARD_SIZE*BOARD_SIZE); i++) *board++='x';
}

void printBoard(char *board) {
    for (int i = 0; i < BOARD_SIZE; i++){
        for (int j = 0; j < BOARD_SIZE; j++){
            //Access the 2D array like this (y * width of array + x)
            printf("%c", board[i*BOARD_SIZE+j]);
            printf(" ");
        }
        printf("\n");
    }
}

//Don't start a name using capitals.. Later when you program c++ or similar you will understand :-)
int main()
{
    //This is a more dynamic memory model and is not allocated on the stack.. (free when done!!)
    char *board=(char*)malloc(BOARD_SIZE*BOARD_SIZE);
    //there are several ways of working with arrays.. No need to complicate stuff if not needed.
    //Just point out the first byte of the array.. (See the methods takes a char pointer and that is what's passed the methods)
    if (board) {
        resetBoard(board);
        //Test to see if it works
        board[1*BOARD_SIZE+2]='0';
        printBoard(board);
        free(board);
    } else {
        printf("Out of memory!\n");
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

或者像 2020 年一样炫耀它!

#define B 16 //Define the size of the 2d matrix
void printMatrix(char*b){for(int i=-1;i<B*B-1;(++i+1)%B?printf("%c ",b[i%B*B+i/B]):printf("%c\n",b[i%B*B+i/B])){}} //Print the 2d matrix
int main(){
    char b[B*B]={[0 ...B*B-1]='x'}; //Reserve the 2d matrix space and set all entries to 'x'
    printMatrix(&b[0]); //pass the pointer to the print 2d matrix method
    return 0; //Done!
}

或 2021 ;-) (二维数组)

#define B 32
void p(char b[B][B]){for(int i=-1;i<B*B-1;(++i+1)%B?printf("%c ",b[i%B][i/B]):printf("%c\n",b[i%B][i/B])){}}
int main(){
    char b[B][B]={[0 ...B-1][0 ...B-1]='x'};
    p(&b[0]);
}
于 2018-11-28T19:11:50.033 回答
1

以下是我之前建议的指针版本:

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

#define Size 8

void resetBoard(char *board, int size);
void printBoard(char *board, int size);

int main()
{
    char *Board = (char *)malloc(Size*Size*sizeof(char));
    resetBoard(Board, Size);
    printBoard(Board, Size);
    printf("\n");
   free(Board);

    return 0;
}

void resetBoard(char *board, int size)
{
    for (size_t i = 0; i < size; i++)
    {
        for (size_t j = 0; j < size; j++)
        {
            *(board +i*size + j) = 'x';
        }
    }
 }

 void printBoard(char *board, int size)
 {
      for (size_t i = 0; i < size; i++)
      {
           for (size_t j = 0; j < size; j++)
           {
                printf("%c ", *(board +i*size + j));
           }
           printf("\n");
      }
  }

编译(在我的机器上)再次看起来像:

gcc -std=c11 -Wall reversi.c -o a.out

执行给出:

./a.out
x x x x x x x x
x x x x x x x x 
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
于 2018-11-28T20:37:46.633 回答
1

@RotemHeinig。你的代码有很多不足之处。贝娄是一个重新改造你的例子的例子。也许,它会让您了解如何改进工作:

#include<stdio.h>

#define Size 8

void resetBoard(char board[][Size]);
void printBoard(char board[][Size]);

int main()
{
    char Board[Size][Size];
    resetBoard(Board);
    printBoard(Board);
    printf("\n");

    return 0;
}

void resetBoard(char board[][Size])
{
    for (size_t i = 0; i < Size; i++)
    {
        for (size_t j = 0; j < Size; j++)
        {
            board[i][j] = 'x';
        }
    }
 }

 void printBoard(char board[][Size])
 {
     for (size_t i = 0; i < Size; i++)
     {
         for (size_t j = 0; j < Size; j++)
         {
              printf("%c ", board[i][j]);
              printf(" ");
         }
         printf("\n");
     }
  }

在我的机器上使用gcc编译此代码如下所示:

gcc -std=c11 -Wall reversi.c -o a.out

执行给出:

./a.out
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x
x x x x x x x x

我希望你能从这里找到灵感。

于 2018-11-28T19:36:45.590 回答